I have this ASP:NET MVC Razor View which has a IEnumerable as model .
I'm creating a table where each line represents a item from the IEnumerable.
I'm using this code:
@foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="selectedFoo" value="@item.isAdded"
@(Html.Raw(item.isAdded? "checked=\"checked\"" : "")) />
@item.FooName
</td>
<td>
@Html.EditorFor(modelItem=> item.Name, new { htmlAttributes = new { @class = "form-control", style = "width: 70px" } })
@Html.ValidationMessageFor(modelItem=> item.Name, "", new { @class = "text-danger" })
</td>
</tr>
}
My problem is that when I enter an incorrect value for the "Name" property all the other text input get the validation error.
Solutions?
Thank you.
I would remove '@:' from the table elements. Also using the razor syntax I would generate a form tag. That may have something to do with it. Also add the ValidationSummary method. Is that snippet you posted the complete view?
If it's a partial view make sure the property 'Name' doesn't exist on any other fields in the view. 'Name' is very generic, perhaps you should rename it something more descriptive. 'FullName', 'ProductName'. ect....