我在asp.net核心中存在模型绑定问题。我有一个模型,其中包含属性和另一个模型的列表。当我提交要显示的模型时,它可以正常工作。我使用调试器跟踪值,并且当我通过表单提交执行发布请求时,剃刀视图中的模型变为空。请帮助我。
我目前正在使用Asp.net core 2.2 MVC,并且正在使用asp-for标记将form属性链接到模型。
<div class="row">
<form asp-action="Edit" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input hidden asp-for="@Model.UserId" />
<div class="col-sm-5 col-md-4 col-lg-4 col-xl-4">
<div class="form-group">
<label asp-for=@Model.Username></label>
<input readonly asp-for=@Model.Username class="form-control" />
<span asp-validation-for=@Model.Username class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for=@Model.Name></label>
<input readonly asp-for=@Model.Name class="form-control" />
<span asp-validation-for=@Model.Name class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for=@Model.Email></label>
<input readonly asp-for=@Model.Email class="form-control" />
<span asp-validation-for=@Model.Email class="text-danger"></span>
</div>
</div>
<div class="row ml-1 form-group">
<div class="col-sm-2 col-md-2 col-lg-2">
Template
</div>
<div class="col-sm-2 col-md-2 col-lg-2">
Entity
</div>
<div class="col-sm-8 col-md-8 col-lg-8 ">
Role Assigned
</div>
@for (int i= 0; i < Model.EntityUserList.Count(); i++)
{
<input hidden asp-for="@Model.EntityUserList[i].TemplateId" />
<div class="my-2 col-sm-2 col-md-2 col-lg-2">
@Model.EntityUserList[i].TemplateName
</div>
<input hidden asp-for="@Model.EntityUserList[i].EntityId" />
<div class="my-2 col-sm-2 col-md-2 col-lg-2">
@Model.EntityUserList[i].EntityTitle
</div>
<div class="col-sm-8 col-md-8 col-lg-8 row ml-1">
<div class="custom-control custom-checkbox my-2 col-sm-2 col-md-2 col-lg-2">
<input type="checkbox" class="custom-control-input" asp-for="@Model.EntityUserList[i].IsSubmitter">
<label class="custom-control-label" asp-for="@Model.EntityUserList[i].IsSubmitter">
Submitter
</label>
</div>
<div class="custom-control custom-checkbox my-2 col-sm-2 col-md-2 col-lg-2">
<input type="checkbox" class="custom-control-input" asp-for="@Model.EntityUserList[i].IsApprover">
<label class="custom-control-label" asp-for="@Model.EntityUserList[i].IsApprover">
Approver
</label>
</div>
<div class="custom-control custom-checkbox my-2 col-sm-2 col-md-2 col-lg-2">
<input type="checkbox" class="custom-control-input" asp-for="@Model.EntityUserList[i].IsAdmin">
<label class="custom-control-label" asp-for="@Model.EntityUserList[i].IsAdmin">
Admin
</label>
</div>
</div>
}
</div>
<div class="form-group mt-3 ml-3">
<input type="submit" value="Update" class="btn btn-outline-primary mr-3" />
<a asp-controller="UserManagement" asp-action="Index" class="btn btn-outline-info" style="width:auto">
Cancel
</a>
</div>
</form>
当我执行post方法时,模型会抛出一个空引用异常。