Asp.Net MVC:为什么我的视图传递NULL模型回到我的控制器?

我无法弄清楚为什么我的视图只传递一个模型的NULL到我的控制器。 这是一个编辑post的方法。 我用Edit Post方法检查了其他控制器,它们的结构和这个方法一样,并且工作正常。 这似乎只是这个观点和控制者。

这是我的看法:

@model Non_P21_Quote_System_v1._0.Models.gl_code @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Edit</h2> @if (TempData["Message"] != null) { <div style="color:green"> @TempData["Message"] </div><br /> } @if (ViewBag.error != null) { <div style="color:red"> <h3>@ViewBag.error</h3> </div><br /> } @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>gl_code</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.ID) <div class="form-group"> @Html.LabelFor(model => model.GL_code, "GL Code", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.GL_code, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.GL_code, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.GL_description, "Gl Description", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.GL_description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.GL_description, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.expense_type_ID, "Expense", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("expense_type_ID", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.expense_type_ID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.eag, "Employee Account Group", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("eag", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.eag, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "gl_Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } 

这是我的控制器方法:

 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit([Bind(Include = "ID,GL_code,GL_description,expense_type_ID,eag")] gl_code gl_code) { if (ModelState.IsValid) { db.Entry(gl_code).State = EntityState.Modified; await db.SaveChangesAsync(); return RedirectToAction("gl_Index"); } ViewBag.eag = new SelectList(db.employee_account_group, "ID", "eag_name"); ViewBag.expense_type_ID = new SelectList(db.expense_type, "ID", "type", gl_code.expense_type_ID); return View(gl_code); } 

当我debugging它时,我看到传入的模型的值为NULL。 我在控制器端的Edit方法的参数部分看到了这一点。

它的null是因为你的模型包含一个名为gl_code的属性,并且你也在POST方法中为你的模型gl_code命名了参数。

更改一个或另一个的名称,模型将正确绑定。

内部发生的事情是表单为每个成功的表单控件提交一个名称/值对,在你的情况gl_code=someValueDefaultModelBinder首先初始化模型的新实例。 然后它读取表单值并在模型中find匹配的属性并将其设置为someValue 。 但是它也会在方法参数中find一个匹配,并尝试将参数的值设置为someValue ,从而失败(因为您无法执行gl_code gl_code = "someValue"; ),并且模型将变为null

看起来你的视图模型名为gl_code有一个属性。 在你的控制器中,你也可以将视图模型称为gl_code。

尝试改变这一点。

 public async Task<ActionResult> Edit(gl_code gl_code) 

 public async Task<ActionResult> Edit(gl_code model)