Remote validation with optional model collection fields

Remote validation works fine when I have only one instance of my model in the view.

The problem is that my opinion is related to a set 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 opinion, I have:

@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 a pretty long way to generate a form, can someone improve the syntax for me please?

The following html form is issued:

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

Although the above html looks pretty good (each model in the collection has a unique identifier and name), there is a problem with additional fields during remote validation:

data-val-remote-additionalfields="*.Name,*.Id"

The Id from the first line gets when the remote check is performed on the second line.

+5
1

-, , . EditorTemplates.

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].Id, GridData[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) {
    ...
}
+1

All Articles