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; }
}
以下是我在视图中调用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本身没有任何注释?我在哪里可以正式阅读这个?我在网上找到的只是偶尔的博客,但他们在网上提到了简单的模型,而不是那些与其他对象有关系的模型。典型的外国关键国家与人际关系。
有什么想法吗?
答案 0 :(得分:4)
视图模型RegisterViewModel没有任何“简单”属性,并且MVC框架不呈现复杂属性,除非我们告诉它如何呈现它们。我们必须为EditorFor帮助器创建DisplayFor和编辑器模板的显示模板。有关详细信息,请查看ASP.NET MVC 2 Templates。另一个link。
将您的EditorTemplates放在文件夹中
~/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)
答案 1 :(得分:0)
我认为你的观点是正确的,因为你的ViewModel应该有注释。在我的MVC项目中,我注释了ViewModel,一切正常。
请注意,您的视图对您的数据模型一无所知 - 它只知道您的视图模型。 : - )
编辑:
[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>
这有帮助吗?