使用附加字段对模型集合进行远程验证
当视图上只有一个模型实例时,远程验证工作正常。
问题是当我的观点正在处理模型集合时。这是我的模型:
public class TableFormTestModel
{
public GridRow[] GridData { get; set; }
public class GridRow
{
public Int32 Id { get; set; }
[Required, StringLength(50), Remote("IsNameAvailable", "TableFormTest", "Admin", AdditionalFields = "Id")]
public String Name { get; set; }
}
}
在我看来,我得到:
@model TableFormTestModel
@using (Html.BeginForm())
{
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
for(var i = 0;i<Model.GridData.Length;i++)
{
<div>
@Html.HiddenFor(x => Model.GridData[i].Id)
@Html.TextBoxFor(x => Model.GridData[i].Name)
@Html.ValidationMessageFor(x => Model.GridData[i].Name)
</div>
}
}
这是生成表单的相当长的方法,有人可以为我改进语法吗?
生成以下 html 表单:
<form method="post" action="/Admin/TableFormTest/"> <div>
<input type="hidden" value="1" name="GridData[0].Id" id="GridData_0__Id" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true">
<input type="text" value="abc" name="GridData[0].Name" id="GridData_0__Name" data-val-required="The Name field is required." data-val-remote-url="/Admin/TableFormTest/IsNameAvailable" data-val-remote-additionalfields="*.Name,*.Id" data-val-remote="&#39;Name&#39; is invalid." data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50." data-val="true">
<span data-valmsg-replace="true" data-valmsg-for="GridData[0].Name" class="field-validation-valid"></span>
</div>
<div>
<input type="hidden" value="2" name="GridData[1].Id" id="GridData_1__Id" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true">
<input type="text" value="def" name="GridData[1].Name" id="GridData_1__Name" data-val-required="The Name field is required." data-val-remote-url="/Admin/TableFormTest/IsNameAvailable" data-val-remote-additionalfields="*.Name,*.Id" data-val-remote="&#39;Name&#39; is invalid." data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50." data-val="true">
<span data-valmsg-replace="true" data-valmsg-for="GridData[1].Name" class="field-validation-valid"></span>
</div>
尽管上面的 html 看起来相当不错(集合中的每个模型都有唯一的 id 和名称),但远程验证上的附加字段存在问题:
data-val-remote-additionalfields="*.Name,*.Id"
当在第二行上触发远程验证时,会拾取第一行中的 ID。
Remote validation works fine when I've got just one instance of my model on the view.
Problem is when my view is dealing with collection of models. Here is my model :
public class TableFormTestModel
{
public GridRow[] GridData { get; set; }
public class GridRow
{
public Int32 Id { get; set; }
[Required, StringLength(50), Remote("IsNameAvailable", "TableFormTest", "Admin", AdditionalFields = "Id")]
public String Name { get; set; }
}
}
In my view I've got :
@model TableFormTestModel
@using (Html.BeginForm())
{
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
for(var i = 0;i<Model.GridData.Length;i++)
{
<div>
@Html.HiddenFor(x => Model.GridData[i].Id)
@Html.TextBoxFor(x => Model.GridData[i].Name)
@Html.ValidationMessageFor(x => Model.GridData[i].Name)
</div>
}
}
This is quite a long way of generating the form, can anyone improve the syntax for me please?
Following html form is produced :
<form method="post" action="/Admin/TableFormTest/"> <div>
<input type="hidden" value="1" name="GridData[0].Id" id="GridData_0__Id" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true">
<input type="text" value="abc" name="GridData[0].Name" id="GridData_0__Name" data-val-required="The Name field is required." data-val-remote-url="/Admin/TableFormTest/IsNameAvailable" data-val-remote-additionalfields="*.Name,*.Id" data-val-remote="'Name' is invalid." data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50." data-val="true">
<span data-valmsg-replace="true" data-valmsg-for="GridData[0].Name" class="field-validation-valid"></span>
</div>
<div>
<input type="hidden" value="2" name="GridData[1].Id" id="GridData_1__Id" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true">
<input type="text" value="def" name="GridData[1].Name" id="GridData_1__Name" data-val-required="The Name field is required." data-val-remote-url="/Admin/TableFormTest/IsNameAvailable" data-val-remote-additionalfields="*.Name,*.Id" data-val-remote="'Name' is invalid." data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50." data-val="true">
<span data-valmsg-replace="true" data-valmsg-for="GridData[1].Name" class="field-validation-valid"></span>
</div>
Although above html looks fairly well ( each Model from the collection has got unique id and name ) there is a problem with additional fields on remote validation :
data-val-remote-additionalfields="*.Name,*.Id"
Id from the first row gets picked up when remote validation is fired on the second row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,是的,您可以改进视图的语法。使用编辑器模板。
创建
Views\Shared\EditorTemplates\GridRow.cshtml
:现在你的主视图只需要是:
至于RemoteAttribute麻烦,这很棘手。该问题是由 MVC 为数组创建的输入名称引起的。如您所见,您的输入已命名,例如
GridData[1].Id
、GridData[1].Name
(等)。好吧,jQuery 通过将这些名称提供给查询字符串来进行 ajax 调用。因此,最终被调用的就是
……
默认的模型绑定器确实不知道如何处理它。
我的建议是编写您自己的自定义模型绑定器。告诉 MVC 如何读取这个查询字符串,然后创建你想要的对象。
这是一个概念验证。 (但不要在生产中使用这个东西:它需要太多假设,并且会因任何意外而崩溃。)
然后,告诉您的
IsNameAvailable
方法使用此模型绑定程序:Firstly, yes, you can improve the syntax of your view. Use EditorTemplates.
Create
Views\Shared\EditorTemplates\GridRow.cshtml
:Now your main view only needs to be:
As for RemoteAttribute troubles, it's tricky. The problem is due to the names of the inputs that MVC creates for arrays. As you can see, your inputs are named, eg,
GridData[1].Id
,GridData[1].Name
(etc). Well, jQuery makes its ajax call by supplying those names to the querystring.Thus, what ends up getting called is
aka
...and the default model binder really doesn't know what to do with that.
What I suggest is to write your own custom model binder. Tell MVC how to read this query string and then make the object you want.
Here's a proof-of-concept. (But do not use this thing in production: it takes too many assumptions and will crash and burn on anything unexpected.)
Then, tell your
IsNameAvailable
method to use this model binder: