Consider the following Razor code
foreach(Person p in Model.Persons){
<form>
@Html.TextBoxFor(m => p.Name)
@Html.ValidationMessageFor(m => p.Name)
@Html.TextBoxFor(m => p.Surname)
@Html.ValidationMessageFor(m => p.Surname)
<button type="submit">Save</button>
</form>
}
In the processed .html
each Person
will have an identical name
attribute with each other. This leads to a wrong behaviour when the validation does not succeed: each Person will have an error message even if I was editing only one of them.
What would be the best way to resolve this issue?
My Post handler is as follows
[HttpPost]
public IActionResult SavePerson(Person person){
// ...
}
and it’d be great to leave it as it is.