客户端验证和集合或动态表单
我正在使用 ASP.NET MVC 2.0。
我需要实现一个动态表单,以便添加“动态”新表单表行。
我的 ViewModel 根据将添加到表单的每一行包含一个 ICollection。
现在我如何使用该机制进行客户端验证?
I'm using ASP.NET MVC 2.0.
I need to implement a dynamic form in order to add "on the fly" new form table row.
My ViewModel contains an ICollection according to each rows which will be added to the form.
Now how can i make client side validation with that mechanism ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将您的模型绑定到列表。
请查看:http://haacked .com/archive/2008/10/23/model-binding-to-a-list.aspx(Phil Haack 是 Microsoft MVC 人员之一)。这基本上是微软人员为了实现该功能而进行的黑客攻击(Phill 声称他们正在设计其他一些方法来实现此目的,但目前我们就是这样做的)。您可以轻松地为此添加验证控件。当您实施命名方案时请非常仔细地查看,并且不要忘记放置隐藏的“索引”字段。索引的值可以是您喜欢的任何值(不必是序列中的数字),但是您在隐藏字段中作为值输入的任何内容都必须位于后面输入控件的方括号内,就像 Phill 中的那样例子。仔细检查一下,我曾经一度陷入了纯粹的痛苦,因为我忘记放置隐藏索引,因为很容易错过一些东西。此外,命名方案的第二部分(“名称”和“价格”)必须与模型中的相同。
完成后,使用 jQuery 动态添加/删除表单上的项目。请注意,jQuery 选择器阻止您对 id 使用方括号,因此您可以以其他方式构造输入控件的“id”属性(即“something_index_propertyName”),同时根据命名方案保留“value”属性。
快乐编码。
You should bind your model to the list.
Please look at the: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx (Phil Haack is one of the Microsoft MVC people). This is basically a hack Microsoft guys made to allow for the functionality (Phill claims that they are designing some other way of doing this, but for now this is how we do it). You can easily add a validation control to this. Please look VERY carefully when you implement the naming scheme, and do NOT forget to put the hidden 'Index' field. Value of index can be anything you like (it doesn't have to be numbers in the sequence) but whatever you put in the hidden field as a value has to be within the square brackets for input controls that follow, just like in the Phill's example. Double check it, I once came to the point of pure misery because I forgot to put the hidden Index, as it is very easy to miss something. Also, the second part of the naming scheme ('Name' and 'Price') has to be the same as in your model.
When you're done with that, use jQuery to dynamically add/remove items on your form. Please note that jQuery selector prevent you from using square brackets for id's, so you can construct 'id' attributes of the input controls in some other way (i.e. 'something_index_propertyName') while you keep the 'value' attribute according to the naming scheme.
Happy coding.