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; }
}
以下是这些类:
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; }
}
这是我在我的 View 中调用 RegisterViewModel 的方法:
@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>
}
这个错误是因为我的 ViewModel 本身没有任何注释而导致的吗?我可以在哪里正式阅读此内容?我在网上找到的只是偶尔的一两个博客,但他们在网上提到了简单的模型,而不是与其他对象有关系的模型,例如这个。典型的外国关键国与人之间的关系。
有什么想法吗?
最佳答案
View 模型 RegisterViewModel 没有任何“简单”属性,并且 MVC 框架不会渲染复杂的属性,除非我们告诉它如何渲染这些属性。我们必须为 DisplayFor 创建显示模板,为 EditorFor 助手创建编辑器模板。检查ASP.NET MVC 2 Templates了解详情。另link .
将编辑器模板放入文件夹中
~/Views/Shared/EditorTemplates
or
~/Views/ControlerName/EditorTemplates
注册模型.cshtml
@model RegisterModel
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
...
...
CityModel.cshtml
@model CityModel
@Html.LabelFor(model => model.CityId)
@Html.EditorFor(model => model.CityId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
CountryModel.cshtml
@model CountryModel
@Html.LabelFor(model => model.CountryId)
@Html.EditorFor(model => model.CountryId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
关于asp.net-mvc-3 - MVC3 Html.EditorFor 没有在我的 View 中渲染任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7085308/