如何将项目添加到“List”在模型“与TextBoxFor,以方便不显眼的客户端验证MVC3?
我在视图模型中使用一个列表,我希望使用不显眼的客户端验证模型来验证视图(使用 MVC3 上的 Razor 视图)。
我正在尝试从表单中收集新的人员信息并进行验证,然后将其添加到视图模型中的列表中。但是使用 TextBoxFor 我别无选择,只能使用集合中的特定项目,这不是它必须的工作方式。
任何帮助表示赞赏。
提前致谢。
public class Person
{
[Required(ErrorMessage="First name is a Required Field")]
public string FirstName
{ get; set; }
[Required(ErrorMessage = "Last name is a Required Field")]
public string LastName { get; set; }
[Required(ErrorMessage = "Primary E-Mail is a Required Field")]
public string PrimaryEmail { get; set; }
public string PrimaryPhoneNumber { get; set; }
}
I am using a List in my viewmodel that I wish to validate on the view(using Razor view on MVC3) using unobtrusive client validation with models.
I am trying to collect a new person info from form with validation and then adding it to the list in the view model. But using the TextBoxFor I have no choise but to use specific item in the collection which is not how it must work.
Any help is appreciated.
Thanks in advance.
public class Person
{
[Required(ErrorMessage="First name is a Required Field")]
public string FirstName
{ get; set; }
[Required(ErrorMessage = "Last name is a Required Field")]
public string LastName { get; set; }
[Required(ErrorMessage = "Primary E-Mail is a Required Field")]
public string PrimaryEmail { get; set; }
public string PrimaryPhoneNumber { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过向 ViewModel 添加一个属性来实现此目的,该属性公开一个新的 person 实例。
你甚至不需要给它赋值,属性的存在就足够了。
然后在您看来:
最后在您接收新数据的操作中:
I've done this by adding a property to my ViewModel which exposes a new person instance.
You don't even have to assign a value to it, the existence of the property is enough.
Then in your view:
and finally in your action that receives the new data: