I’m trying to populate a table with member information such as name, email etc. I also have columns for entering a "warning" for members, and a textbox where more information about this warning is entered. You’re then supposed to press a button that posts this information, where it is then bound to a model and stored in a database.
By going the normal way and looping over the list and generating inputs for this based on the model, I run into issues (which I assume is due to the fact that there are multiple members in this table who all have the same inputs. This is the relevant code that I used in the MemberView.cshtml page:
@foreach (var user in Model.memberList)
{
<form asp-for="WebWarning" method="post">
<td>Regular member</td>
<td>
<select asp-for="WebWarning.WarningType" asp-items="Model.WarningTypes")"></select>
</td>
<td>
<input asp-for="WebWarning.Message"/>
</td>
<td>
<input asp-for="WebWarning.Person" type="hidden" value="@user" />
<button type="submit" class="btn btn-warning">Add Warning</button>
</td>
</form>
}
Would anyone know how to get this to work? I’ve been trying (and failing) for hours now.
2
Answers
I’m not sure if this is what you need, but you are missing the closing curly brace after your If Statement.
From your description in the question, I think you wanna bind multiple values with the same name. In this case, We usually set the index manually. Because you don’t provide your model design, So here I write a simple demo, Hope it can help you solve this issue.
Model
Controller
View
Now I input the value into
input
tag and it will the value successfully.