當我在視圖上只有一個模型實例時,遠程驗證正常工作。在模型集合上使用AdditionalFields進行遠程驗證
問題是當我的視圖處理模型的集合。這裏是我的模型:
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會被拾取。
嘗試使用DisplayTemplates,然後使用@Html。DisplayFor(model => model.GridData) – mimo 2014-02-06 14:03:39