Asp.net mvc 3 MVC3 Html.EditorFor isn';在我看来,我一件事也看不出来
以下是我在视图中如何调用RegisterViewModel:Asp.net mvc 3 MVC3 Html.EditorFor isn';在我看来,我一件事也看不出来,asp.net-mvc-3,templates,model,editorfor,Asp.net Mvc 3,Templates,Model,Editorfor,以下是我在视图中如何调用RegisterViewModel: public class RegisterModel { [Required] [Display(Name = "Usuario")] public string UserName { get; set; } [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Correo")] public string
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; }
}
@model foodigly.WebUI.Models.RegisterViewModel
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
RegisterViewModel
@Html.EditorForModel()
}
这个错误是因为我的ViewModel本身没有任何注释造成的吗?我在哪里可以正式了解到这一点?我在网上找到的只是一两个偶然的博客,但他们在网上提到了一些简单的模型,而不是那些与其他对象有关系的模型。典型的外键国家对个人关系
有什么想法吗?我认为您的ViewModel应该有注释,这是对的。在我的MVC项目中,我对ViewModel进行了注释,一切正常 请记住,您的视图对您的数据模型一无所知-它只知道您的视图模型。:-) 编辑:
[DataMember]
公共类用户{
公共字符串电话号码
{ ... }
}
//[视图模型]
公共类ViewUser{
[必需,正则表达式(@“^\((\d{3})\)?[-](\d{3})[-]?(\d{4})$”,ErrorMessage=“请输入有效的10位电话号码”)]
公共字符串ViewPhoneNumber{
}
}
查看页面
@EditorFor(model=>model.Phone1)
@Html.ValidationMessageFor(model=>model.Phone1,“请输入有效的电话号码”)
这有帮助吗?视图模型RegisterViewModel没有任何“简单”属性,MVC框架也不会呈现复杂属性,除非我们告诉它如何呈现这些属性。我们必须为DisplayFor创建显示模板,为EditorFor助手创建编辑器模板。详细信息请查看。另一个 将编辑器模板放在文件夹中
[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>
RegisterModel.cshtml
~/Views/Shared/EditorTemplates
or
~/Views/ControlerName/EditorTemplates
@model RegisterModel
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
...
...
@model CityModel
@Html.LabelFor(model => model.CityId)
@Html.EditorFor(model => model.CityId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
CityModel.cshtml
~/Views/Shared/EditorTemplates
or
~/Views/ControlerName/EditorTemplates
@model RegisterModel
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
...
...
@model CityModel
@Html.LabelFor(model => model.CityId)
@Html.EditorFor(model => model.CityId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
CountryModel.cshtml
~/Views/Shared/EditorTemplates
or
~/Views/ControlerName/EditorTemplates
@model RegisterModel
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
...
...
@model CityModel
@Html.LabelFor(model => model.CityId)
@Html.EditorFor(model => model.CityId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
嗯,嗯。。。你能说说你的注释吗?它不会马上向我扑过来,当然。我们使用EF4来创建数据对象。我们不想对它们进行注释,因为如果我们更新edmx,那么注释将被覆盖。要显示一个简化示例,请执行以下操作: