使用附加字段对模型集合进行远程验证

发布于 2024-10-28 15:46:03 字数 2721 浏览 1 评论 0原文

当视图上只有一个模型实例时,远程验证工作正常。

问题是当我的观点正在处理模型集合时。这是我的模型:

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="&amp;#39;Name&amp;#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="&amp;#39;Name&amp;#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="&#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>

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-11-04 15:46:03

首先,是的,您可以改进视图的语法。使用编辑器模板

创建Views\Shared\EditorTemplates\GridRow.cshtml

@model TestMvc.Models.TableFormTestModel.GridRow
<div>
    @Html.HiddenFor(x => x.Id)
    @Html.TextBoxFor(x => x.Name)
    @Html.ValidationMessageFor(x => x.Name)
</div>

现在你的主视图只需要是:

@model TableFormTestModel
@using (Html.BeginForm())
{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();
    @Html.EditorFor(x => x.GridData)
}

至于RemoteAttribute麻烦,这很棘手。该问题是由 MVC 为数组创建的输入名称引起的。如您所见,您的输入已命名,例如 GridData[1].IdGridData[1].Name (等)。好吧,jQuery 通过将这些名称提供给查询字符串来进行 ajax 调用。

因此,最终被调用的就是

/Admin/TableFormTest/IsNameAvailable?GridData%5B1%5D.Name=sdf&GridData%5B1%5D.Id=5 

……

/Admin/TableFormTest/IsNameAvailable?GridData[1].Name=sdf&GridData[1].Id=5 

默认的模型绑定器确实不知道如何处理它。

我的建议是编写您自己的自定义模型绑定器。告诉 MVC 如何读取这个查询字符串,然后创建你想要的对象。

这是一个概念验证。 (但不要在生产中使用这个东西:它需要太多假设,并且会因任何意外而崩溃。)

public class JsonGridRowModelBinder : IModelBinder {

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
        var model = new TableFormTestModel.GridRow();
        var queryString = controllerContext.HttpContext.Request.QueryString;
        model.Name = queryString[queryString.AllKeys.Single(x => x.EndsWith("Name"))];
        string id = queryString[queryString.AllKeys.Single(x => x.EndsWith("Id"))];
        model.Id = string.IsNullOrWhiteSpace(id) ? 0 : int.Parse(id);

        return model;
    }

}

然后,告诉您的 IsNameAvailable 方法使用此模型绑定程序:

public JsonResult IsNameAvailable([ModelBinder(typeof(JsonGridRowModelBinder))] TableFormTestModel.GridRow gridRow) {
    ...
}

Firstly, yes, you can improve the syntax of your view. Use EditorTemplates.

Create Views\Shared\EditorTemplates\GridRow.cshtml:

@model TestMvc.Models.TableFormTestModel.GridRow
<div>
    @Html.HiddenFor(x => x.Id)
    @Html.TextBoxFor(x => x.Name)
    @Html.ValidationMessageFor(x => x.Name)
</div>

Now your main view only needs to be:

@model TableFormTestModel
@using (Html.BeginForm())
{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();
    @Html.EditorFor(x => x.GridData)
}

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

/Admin/TableFormTest/IsNameAvailable?GridData%5B1%5D.Name=sdf&GridData%5B1%5D.Id=5 

aka

/Admin/TableFormTest/IsNameAvailable?GridData[1].Name=sdf&GridData[1].Id=5 

...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.)

public class JsonGridRowModelBinder : IModelBinder {

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
        var model = new TableFormTestModel.GridRow();
        var queryString = controllerContext.HttpContext.Request.QueryString;
        model.Name = queryString[queryString.AllKeys.Single(x => x.EndsWith("Name"))];
        string id = queryString[queryString.AllKeys.Single(x => x.EndsWith("Id"))];
        model.Id = string.IsNullOrWhiteSpace(id) ? 0 : int.Parse(id);

        return model;
    }

}

Then, tell your IsNameAvailable method to use this model binder:

public JsonResult IsNameAvailable([ModelBinder(typeof(JsonGridRowModelBinder))] TableFormTestModel.GridRow gridRow) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文