Problems with an "edit in place" form and model bi...
# help-with-other
c
I have a set of huge tables where the rows are rendered from a foreach loop. There are items on each row that are input fields which the user can change and then save the whole form. My problem is that if I use model binding (asp tag helpers) then each separate item in a row gets the same name and id as the same item in each row. If I switch over to not using asp helpers then in the controller I don't get any of the inputs posted from the rows, just the hidden fields in the form outside the rows. I've looked around and can't see a way of using taghelpers in this scenario. I can't generate a model class for the whole table itself (instead of a Model for the form and a row subclass) as there are many tables all with different numbers of rows, (but same elements per row). Does anyone have any suggestions or insight into the tag helper situation as that's what I'd prefer to use if I can? Thanks.
d
Have you tried using a
for
loop instead of a
foreach
? I think if you access your property by index, then your fields also get the index in their name when you use the tag helpers
h
+1 for @D_Inventor I'm pretty sure I have some code, will have a quick dig
maybe a bit more complicated than you need, but should provide the general idea
c
Ah, but that doesn't use the asp-for tag helper. I was wanting something that would post the form data in the same Model as it arrived, i.e. with a List for the individual rows of the table. If it's even possible, that is.
Currently posting back to
public IActionResult Edit(Microsoft.AspNetCore.Http.IFormCollection tableData) {...... lots of manipulation into a Model.......then spin through the List<row> and saving each one.}
h
I think you can still do this
<input asp-for="@Model.Categories[ii].Name" />