I tried following this similar question: How to update multiple rows at once from a View (ASP.NET - Core) Everything is almost structurally similar but the data from the view still could not go to the controller.
Here is the view model:
public class ManageUsersViewModel
{
public Guid? ProjectId;
public List<ProjectUserRowModel> ProjectUserRows;
public List<ProjectRole> ProjectRoles;
}
public class ProjectUserRowModel
{
public Guid Guid { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Guid RoleId { get; set; }
}
Here is the view (Just the form/table for brevity):
<div >
<div >
<form asp-action="ManageUsersSave" method="post">
<div asp-validation-summary="ModelOnly" ></div>
<input type="hidden" asp-for="ProjectId"/>
<table>
@for (var i=0; i < Model.ProjectUserRows.Count(); i )
{
<tr>
<input type="hidden" asp-for="@Model.ProjectUserRows[i].Guid" />
<td>
<input type="hidden" asp-for="@Model.ProjectUserRows[i].Name" />
@Model.ProjectUserRows[i].Name
</td>
<td>
<input type="hidden" asp-for="@Model.ProjectUserRows[i].Email" />
@Model.ProjectUserRows[i].Email
</td>
<td>
<select asp-for="@Model.ProjectUserRows[i].RoleId" asp-items="@(new SelectList(Model.ProjectRoles, "ProjectRoleId", "Name", @Model.ProjectUserRows[i].RoleId))">
<option value="">-- Project Role</option>
</select>
</td>
<td>
<a asp-action="DeleteProjectUser" asp-route-id="@Model.ProjectUserRows[i].Guid" >Delete</a>
</td>
</tr>
}
</table>
<div >
<input type="submit" value="Save" />
</div>
</form>
</div>
</div>
And this is the declaration of the method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ManageUsersSave([Bind("ProjectId,ProjectUserRows,ProjectRoles")]ManageUsersViewModel manageUsersViewModel)
I could not find any errors at all except that the values when processing the object, the values are null.
CodePudding user response:
I notice you are using fields, not properties in the ManageUsersViewModel
model.
The model binder only works for properties.
Also a note on the [Bind]
attribute. I'm not very familiar with it, whether it recognizes form data or not. If it doesn't work, use the [FromForm].
CodePudding user response:
What happens if you specify FromBody instead of Bind?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ManageUsersSave([FromBody]ManageUsersViewModel manageUsersViewModel)