且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用单个视图模型在MVC Razor视图上发布多个表单

更新时间:2022-03-09 21:46:04

我建议使每个表单成为局部视图,并仅传入父模型所需的子模型

I suggest make each form a partial view and pass in only the sub model needed from the parent model

 @{await Html.RenderPartialAsync("AViewPartial", Model.AViewModel); }

然后您可以将部分输入更改为

then your inputs in the partial can be changed from

<input asp-for="AViewModel.Name" class="form-control"/>

<input asp-for="Name" class="form-control"/>

这应该使它与Modelbinder正确绑定,以便将其传递给您的动作

which should make it bind correctly by the modelbinder so it gets passed into your action