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

 2023-02-17    488  

问题描述

我无法弄清楚为什么我的视图只传递给我的控制器的模型的空缺.
这是用于编辑POST方法.我检查了其他控制器,编辑后的方法是与此相同的方式构建的方法,它们正常工作.它似乎只是这个视图和控制器.

这是我的观点:

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

@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);
}

当我调试它时,我会看到在值为null中传递的模型.我在编辑方法的参数部分的控制器侧看到了它.

推荐答案

它的null,因为您的模型包含名为gl_code的属性,并且您也在post方法中命名为model gl_code的参数.

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

在内部发生的事情是表单在案例中为每个成功的表单控件提交名称/值对,在您的情况下gl_code=someValue. DefaultModelBinder首先初始化模型的新实例.然后,它读取表单值,并在模型中找到属性的匹配,并将其设置为someValue.但它还在方法参数中找到一个匹配项,并尝试将参数的值设置为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)

以上所述是小编给大家介绍的Asp.Net MVC: 为什么我的视图会把NULL模型传回给我的控制器?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34181.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。