I am trying to use EditorFor to enter values into a list within a view model. I don't want every element in the list to allow edits though. The method of determining which element in the list can be edited is by using an additional list in my view model. The two lists line up perfectly so at the i'th position of one list exactly corresponds to the i'th position of the other. The code is as follows:
<div class="form-group">
@Html.Label("Inputs", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@for (int i = 0; i < Model.InputList.Count(); i++)
{
@Html.Label(i + 1 + ") " + Model.ProtocolSteps[i].Description)
if (Model.ProtocolSteps[i].InputNeeded.Value)
{
@Html.EditorFor(model => model.InputList[i], new { htmlAttributes = new { @class = "form-control" } })
}
<br />
}
</div>
</div>
The ProtocolSteps list determines if an editorfor template should be displayed. If it should, the editorfor input is stored into the InputList. This code works perfectly without the if statement (i.e. all values are stored to the correct location in InputList). With the if statement, the view is displayed perfectly, but values are not stored into InputList (usually one value is stored into the list, but not all). Any help is appreciated with why the if statement is messing things up.