mvc :控制特定所需的验证
我正在使用 mvc 2.0 和 C#.Net 在我的视图页面中,我正在创建多个具有所需字段验证的控件。为了绘制我的控件,我使用 for 循环遍历我的模式对象,它实际上是业务对象的集合。对于代表“用户评论”的 TextBox 控件,我在其下方放置了验证。 当
<%:Html.TextBoxFor(model=>model.mycollection[i].Comment %>
<%:Html.ValidationMessageFor(model => model.mycollection[i].comments) %>
每行(每个评论文本框)都有一个提交按钮,如下所示
<input runat="Server" name="Button" id="Submit1" type="submit" class="button" value="save comments">
加载表单时,会创建 3 行,每行包含一组文本框和一个要提交的按钮。这是因为该模型是 3 个对象的集合。所以现在我可以单独提交用户输入的每条评论。但问题是,当我单击任何按钮时,它不是验证相应的文本框以进行所需的验证,而是一次验证页面上的所有文本框并为所有文本框提供错误消息。如何避免这种行为? Mypage 顶部有一个 html.beginform 方法,可将所有内容放入一个表单中并将提交发布到控制器操作。
I am using mvc 2.0 with C#.Net
In my view page, I am creating multiple controls with required field validations. to plot my controls, I am using for loop through my mode object which is actually a collection of business object. For my TextBox control which represent 'user comments', I have put validation under it. like
<%:Html.TextBoxFor(model=>model.mycollection[i].Comment %>
<%:Html.ValidationMessageFor(model => model.mycollection[i].comments) %>
There is a sumit button for each row (for each comment textbox) like below
<input runat="Server" name="Button" id="Submit1" type="submit" class="button" value="save comments">
When the form loads, 3 rows are created, each containing a set of text box and a button to submit. This is because the model is a collection of 3 objects. So now I can submit each comments entered by user individually. But the problem is, when I click on any button, instead of validating corresponding textbox for required validation, it validates all of the textboxes on page at a time and gives error message for all of them. How to avoid this kind of behaviour? Mypage has a html.beginform method on top to put everything in a single form and to post the submit to an controller action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否可以尝试将集合中的每个对象放入单独的表单中,如下所示:
..这样,当您单击“提交”时,只有该表单才会被验证。
您也可以使用 div 代替 table 。
Can you try to put each object from your collection in a separate form like this:
..so then only that form will be validated when you click submit.
Also you can use divs instead of table .