MVC3 Html.EditorFor doesn't do a single thing in my view

public class RegisterViewModel { //Plain POCO Properties public RegisterModel RegisterModel { get; set; } //Meant to be used in SelectLists. public IEnumerable<CityModel> Cities { get; set; } public IEnumerable<CountryModel> Countries { get; set; } } 

These classes are:

 public class RegisterModel { [Required] [Display(Name = "Usuario")] public string UserName { get; set; } [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Correo")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Contraseña")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirme Su Contraseña")] [Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")] public string ConfirmPassword { get; set; } [Required] [Display(Name = "Nombre")] public string Nombre { get; set; } [Required] [Display(Name = "Apellido")] public string Apellido { get; set; } [Required] [Display(Name = "Direccion")] public string Address { get; set; } [Required] [Display(Name = "Telefono Fijo")] public string Telephone { get; set; } [Required] [Display(Name = "Celular")] public string MobilePhone { get; set; } [Required] [Display(Name = "Fecha de Nacimiento")] public DateTime DateOfBirth { get; set; } [Required] [Display(Name = "Soy:")] public bool Sex { get; set; } [Required] [Display(Name = "Carnet")] public int Carnet { get; set; } } public class CityModel { [HiddenInput(DisplayValue = false)] public int CityId { get; set; } [Required] [Display(Name = "Ciudad")] public string Name { get; set; } } public class CountryModel { [HiddenInput(DisplayValue = false)] public int CountryId { get; set; } [Required] [Display(Name = "Pais")] public string Name { get; set; } } 

And this is how I call RegisterViewModel in my view:

 @model Foodiggly.WebUI.Models.RegisterViewModel @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>RegisterViewModel</legend> @Html.EditorForModel() <p> <input type="submit" value="Create" /> </p> </fieldset> } 

Is this error caused because my ViewModel does not have any annotations? Where can I officially read about it? All I managed to find on the Internet is a separate blog or two, but they mention simple models online, and not those that relate to other objects such as this. Typical external connection between countries and people.

Any ideas?

+4
source share
2 answers

The RegisterViewModel has no “simple” properties, and the MVC environment does not display complex properties unless we tell you how to render them. We need to create display templates for DisplayFor and an editor template for the EditorFor helpers. See ASP.NET MVC 2 Templates for more information. Another link.

Put your EditorTemplates in folders

 ~/Views/Shared/EditorTemplates or ~/Views/ControlerName/EditorTemplates 

RegisterModel.cshtml

 @model RegisterModel @Html.LabelForFor(model => model.UserName) @Html.EditorFor(model => model.UserName) @Html.LabelForFor(model => model.Email) @Html.EditorFor(model => model.Email) ... ... 

CityModel.cshtml

 @model CityModel @Html.LabelForFor(model => model.CityId) @Html.EditorFor(model => model.CityId) @Html.LabelForFor(model => model.Name) @Html.EditorFor(model => model.Name) 

CountryModel.cshtml

 @model CountryModel @Html.LabelForFor(model => model.CountryId) @Html.EditorFor(model => model.CountryId) @Html.LabelForFor(model => model.Name) @Html.EditorFor(model => model.Name) 
+4
source

I think you're right that your ViewModel should contain annotations. In my MVC project, I comment on the ViewModel and everything works fine.

Keep in mind that your View knows nothing about your data model - it only knows your view model. :-)

edit:

 [DataMember] public class User { public string PhoneNumber { ... } } //[ViewModel] public class ViewUser { [Required, RegularExpression(@"^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$", ErrorMessage="Please enter a valid 10 digit phone number")] public string ViewPhoneNumber { } } View page <div class="editor-field"> @Html.EditorFor(model => model.Phone1) @Html.ValidationMessageFor(model => model.Phone1, "Please enter a valid phone number") </div> 

Does it help?

0
source

All Articles