MiguelSlv
MiguelSlv

Reputation: 15113

MVC EditorTemplate binding not work for 2 elements in same page

My page displays, side by side, to arrays of objects of class ExternalExeWorkerSettings. To render de objects i created a Editor Template for the 'ExternalExeWorkerSettings'.

The page is correctly populated with elements from both arrays, along the page. When the page is saved, only the elements in the first array are captured. The second array has all instances of 'ExternalExeWorkerSettings' created but all them have the default state. No properties of ExternalExeWorkerSettings are captured for any instance of the second array.

Showing relevant parts of code:

View (page)

@for (int i = 0; i < Model.Count(); i++)
{            
    //Left Array (SettingsViewModelPRD)
    @Html.EditorFor(model => Model[i].SettingsViewModelPRD, null, "ActivityList[" + @i + "].SettingsViewModelPRD", new { htmlAttributes = new {     @class = "editSettings" } })
    @Html.ValidationMessageFor(model => model[i].SettingsViewModelPRD, "", new { @class = "text-danger" })

    //Right Array (SettingsViewModelTST)
    @Html.EditorFor(model => Model[i].SettingsViewModelTST, null, "ActivityList[" + @i + "].SettingsViewModelTST")
    @Html.ValidationMessageFor(model => model[i].SettingsViewModelTST, "", new { @class = "text-danger" })
}

EditorTemplate (ExternalExeWorkerSettings.cshtml)

@model ACT_Workers.Workers.ExternalExeWorkerSettings

@using (Html.BeginForm())
{
<div class="form-horizontal">
    <div class="activity-box">
        <strong>External Exe</strong>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.exeName, htmlAttributes: new { @class = "control-label" })
            <div class="activty-textbox">
                @Html.EditorFor(model => model.exeName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.exeName, "", new { @class = "text-danger" })
            </div>
        </div>
        ...

The rendered html from Editor Template

//SettingsViewModelPRD
<input class="form-control text-box single-line" data-val="true" data-val-maxlength="Max size exceeded" data-val-maxlength-max="255" data-val-minlength="Field is required" data-val-minlength-min="1" data-val-required="The Nome do Executável field is required." id="ActivityList_0__SettingsViewModelPRD_exeName" name="ActivityList[0].SettingsViewModelPRD.exeName" type="text" value="">

//SettingsViewModelTST
//the attribute name is good and the value is correctly populated    
<input class="form-control text-box single-line" data-val="true" data-val-maxlength="Max size exceeded" data-val-maxlength-max="255" data-val-minlength="Field is required" data-val-minlength-min="1" data-val-required="The Nome do Executável field is required." id="ActivityList_0__SettingsViewModelPRD_exeName"name="ActivityList[0].SettingsViewModelPRD.exeName" type="text" value="something">

Yet, at the controller, every object of the SettingsViewModelTST array have all properties set to null. SettingsViewModelPRD array comes correctly populated.

More odd, if i remove the Editor Template and let i generate the html by itself, it works for both arrays.

It looks like a framework bug but this scenario is not atypical. What could be failing here?

Upvotes: 0

Views: 54

Answers (1)

MiguelSlv
MiguelSlv

Reputation: 15113

Remove the @using (Html.BeginForm()) from editor template.

Upvotes: 0

Related Questions