Asp.Net MVC:为什么我的观点传递NULL模型回我的控制器? [英] Asp.Net MVC: Why is my view passing NULL models back to my controller?

查看:115
本文介绍了Asp.Net MVC:为什么我的观点传递NULL模型回我的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白,为什么我的观点仅传回一个空的模式我的控制器。
这是一个编辑Post方法。我检查其他控制器与组建方式这一个相同的,他们做工精细编辑帖子的方法。这似乎是眼前这个视图和控制器。

I cannot figure out why my view only passes back a NULL for a model to my controller. This is for an Edit Post method. I checked other controllers with Edit Post methods that are structured the same way as this one and they work fine. It seems to be just this view and controller.

下面是我的看法:

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

下面是我的控制器的方法:

Here is my controller method:

[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值的。我的编辑方法的参数部分见状控制器侧。

When I debug it, I see the model being passed in is of value NULL. I am seeing this on the controller side at the the parameters part of the Edit method.

推荐答案

其空,因为模型包含一个名为属性 gl_ code ,你也命名为你的模型参数 gl_ code 中的POST方法。

Its null because your model contains a property named gl_code and you have also named the parameter for your model gl_code in the POST method.

改变的一个或另一个的名称,模型将正确结合

Change the name of one or the other and the model will bind correctly.

什么是内部的动作是表单提交的每一个成功的表单控件的名称/值对,你的情况 gl_ code = someValue中。在 DefaultModelBinder 首先初始化模型的新实例。然后读取表单值和找到一个匹配模型中的属性,并将其设置为 someValue中。但它也找到一个匹配的方法参数和尝试设置参数为 someValue中的值,失败(因为你不能做 gl_ code gl_ code =someValue中; )和模型成为

What is happening internally is that the form submits a name/value pair for each successful form control, in your case gl_code=someValue. The DefaultModelBinder first initializes a new instance of your model. It then reads the form values and finds a match for the property in your model and sets it to someValue. But it also finds a match in the method parameters and tries set the value of the parameter to someValue, which fails (because you cannot do gl_code gl_code = "someValue";) and the model becomes null.

这篇关于Asp.Net MVC:为什么我的观点传递NULL模型回我的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆