I am currently using model binding and ASP.NET MVC 3 and .NET 4.0.
View Model Class:
public class BasicViewModel { [Display(Name = @"Names")] [Required(ErrorMessage = @"Names is required")] [DisplayFormat(ConvertEmptyStringToNull = true)] List<string> Names { get; set; } [Display(Name = @"Email")] [Required(ErrorMessage = @"Email is required")] string Email { get; set; } }Controller
[AcceptVerbs(HttpVerbs.Post)] public ActionResult NameEmail( BasicViewModel basicModel) { // some manipulation of data }View in cshtml file (razor view engine)
// model declared here using @model BasivViewModel // only required part shown labels part of code removed @Html.TextBoxFor(model => model.Names) ... @Html.TextBoxFor(model => model.Email) ...
The model binding provided by ASP.NET MVC binds the string Email to null if it is empty but binds the List Names to empty string (""). I want it to be null. I made the binding work using JavaScript by parsing the values of form fields on click of submit button. But i want the asp.net model binding to do this. Furthermore, it would be great if there is some field in Data Annotations class like Required for this functionality. I tried this Null Display Text Property and refer to the remarks section. Is there a solution or is this how it is implemented?. I am not sure whether i have understood this part of model binding correctly.