0

I have a form made up of nested models as below:

foreach (var item in Model)
   {
    <h3>
        @item.StageDescription
    </h3>
    <div class="well">
        <table id="Item@(item.ID)" class="WizardOption">
            <thead>
                <tr>
                    <some headings here />
                </tr>
            </thead>
            <tbody>
                @Html.EditorFor(m => item.WizardOptions,"","WizardOptions",null)
            </tbody>
        </table>
    </div>
}

The WizardOption class has a required field call Display Value:

public class WizardOptionMetaData {
        [Required]
        public string DisplayValue { get; set; }
}

This works fine for the first table, if I leave a DisplayValue field blank I get the error: "The DisplayValue field is required." and the following markup is rendered:

<input class="description-box" data-val="true" data-val-required="The DisplayValue field is required." id="WizardOptions_0__DisplayValue" name="WizardOptions[0].DisplayValue" type="text" value="">

But any tables after the first one don't get the validation rendered properly:

<input class="description-box" id="WizardOptions_1__DisplayValue" name="WizardOptions[1].DisplayValue" type="text" value="">

Where am I going wrong?

Lyno
  • 16
  • 1

1 Answers1

0

Found the answer on a question I didn't find until after I posted the question:

ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout

@{
     var originalContext = ViewContext.FormContext;
     ViewContext.FormContext = new FormContext();
} 

<!-- This will generate proper HTML5 data-* validation attributes -->
@Html.TextBoxFor(x => x.Prop1)
     @Html.ValidationMessageFor(x => x.Prop1)

     @Html.TextBoxFor(x => x.Prop2)
     @Html.ValidationMessageFor(x => x.Prop2)

@{
     ViewContext.FormContext = originalContext;
 }
Community
  • 1
  • 1
Lyno
  • 16
  • 1