Say I have the following list properties being rendered to my create view:
@for (var i = 0; i < Model.SiteSplits.Count(); i++)
{
<div class="clearfix"></div>
<div class="form-group">
@Html.LabelFor(model => model.SiteSplits[i].CostCode, htmlAttributes: new {@class = "control-label col-md-3"})
<div class="col-md-9">
@Html.EditorFor(model => model.SiteSplits[i].CostCode, new {htmlAttributes = new {@class = "form-control"}})
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
@Html.LabelFor(model => model.SiteSplits[i].SplitPercentage, htmlAttributes: new {@class = "control-label col-md-3"})
<div class="col-md-9">
@Html.EditorFor(model => model.SiteSplits[i].SplitPercentage, new {htmlAttributes = new {@class = "form-control"}})
</div>
</div>
}
This results in x3 cost code and split percentages which are passed back to the controller in a list on POST.
I've put a required annotation on the CostCode
and SplitPercentage
in the view model, which is resulting in all 3 of the Split/Codes being required.
Is there any way of making just the CostCode
and SplitPercentage
Required on just the first loop and have it ignore the validation for the second two so they are merely optional?