ASP.NET MVC错误HttpPost创建 - 传递模型POST之后Null值 - 模型绑定问题? [英] ASP.NET MVC Error in HttpPost Create - Null Values Passed in Model after POST - Model Binding Issue?

查看:240
本文介绍了ASP.NET MVC错误HttpPost创建 - 传递模型POST之后Null值 - 模型绑定问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该是什么样一个非常简单的ASP.NET MVC创建方法的问题。

I have a problem with what should be a very simple ASP.NET MVC Create method.

在我填充数据并提交表单在创建视图(以及所有的验证规则传递),传递给POST方法模型的实例将包含null或0值的每个字段和ModelState.IsValid会返回false。

After I populate data and submit the form in the Create view (and all the validation rules are passed), the instance of the model passed to the POST method will contain either null or 0 values for every field and ModelState.IsValid will return false.

我使用Firebug检查POST表单参数,并采取行动显然已发布从田间地头的所有参数提交。然而,这些参数并不必然会传递到控制器POST方法的模型,因此,模型(在code即材料)中包含的所有字段为空值(如使用Watch和断点放置在开始时检查POST方法)。

I use Firebug to check the POST form parameters, and the submit action obviously has posted all the parameters from the fields. However, these parameters are not bound to the model passed to the POST method in the Controller, hence, the model (namely material in the code) contains null values in all field (as checked using Watch and breakpoints placed at the beginning of the POST method).

这是Firebug的POST参数:

POST parameters from Firebug:

Content-Type: application/x-www-form-urlencoded Content-Length: 228 MaterialNoMass.MaterialNoMassID=9mmPlasterboard&MaterialNoMass.RoughnessTypeID=3&MaterialNoMass.Resistance=0.0560&MaterialNoMass.ThermalAbsorptance=0.09&MaterialNoMass.SolarAbsorptance=0.07&MaterialNoMass.VisibleAbsorptance=0.07

创建视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Database.Master"
 Inherits="System.Web.Mvc.ViewPage<MvcDeepaWebApp.ViewModels.DatabaseSimpleMaterialViewModel>" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <% Html.EnableClientValidation(); %>
        <% using (Html.BeginForm())
           {%>
        <%: Html.ValidationSummary(true) %>
        <fieldset>
            <legend>Create MaterialNoMass</legend>
            <%: Html.EditorFor(model => model.MaterialNoMass, new { RoughnessTypes = Model.RoughnessTypes })%>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
        <% } %>
        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>
    </asp:Content>

GET和控制器创建的POST方法:

GET and POST methods of Create in Controller:

    //
    // GET: /DatabaseSimpleMaterial/Create
    public ActionResult Create()
    {
        var viewModel = new DatabaseSimpleMaterialViewModel
        {
            MaterialNoMass = new MaterialNoMass(),
            RoughnessTypes = deepaDB.RoughnessTypes.ToList()
        };

        return View(viewModel);
    }

    //
    // POST: /DatabaseSimpleMaterial/Create

    [HttpPost]
    public ActionResult Create(MaterialNoMass material)
    {
        if (ModelState.IsValid)
        {
            MaterialAllType mt = new MaterialAllType();
            mt.MatID = material.MaterialNoMassID;
            mt.MatTypeID = deepaDB.MaterialTypes.Single(type => type.MatType == "SimpleMaterial").MatTypeID;
            material.MatTypeID = mt.MatTypeID;

            deepaDB.AddToMaterialAllTypes(mt);

            deepaDB.AddToMaterialNoMasses(material);

            deepaDB.SaveChanges();
            return RedirectToAction("Index");
        }

        //Error occurred
        var viewModel = new DatabaseSimpleMaterialViewModel
        {
            MaterialNoMass = material,
            RoughnessTypes = deepaDB.RoughnessTypes.ToList()
        };

        return View(viewModel);
    }

仅供参考,每当创建了新的MaterialNoMass,MaterialAllType的新实例也应使用MaterialNoMass的相同的ID创建的。我还没有达到修改数据库的阶段然而,因为该模型结合似乎是不工作

FYI, whenever a new MaterialNoMass is created, a new instance of MaterialAllType should also be created using the same ID of MaterialNoMass. I haven't even reached the stage of modifying the database yet, since the model binding seems to be not working.

我用类似的应用程序中创建其他控制器的方法,它们都做工精细,除了在这个控制器。我一直在调试这个过去两天辛苦。请帮助!

I've used similar Create approach in other Controllers in the application and they all work fine except in this Controller. I've been having a hard time in debugging this for the past two days. Please help!

推荐答案

这个问题来自于事实,你传递给视图的模型是 DatabaseSimpleMaterialViewModel 和内你期望 MaterialNoMass POST操作。所以一定要使用正确的preFIX:

The problem comes from the fact that the model you are passing to the view is DatabaseSimpleMaterialViewModel and inside the POST action you expect MaterialNoMass. So make sure you use the correct prefix:

[HttpPost]
public ActionResult Create([Bind(Prefix="MaterialNoMass")]MaterialNoMass material)

我想你编辑模板生成这样的字段:

I suppose that you editor template generates fields like this:

<input type="text" name="MaterialNoMass.MaterialNoMassID" value="" />
<input type="text" name="MaterialNoMass.RoughnessTypeID" value="" />
...

所以,你需要指明模型绑定是有这样preFIX。

So you need to indicate the model binder that there's such prefix.

这篇关于ASP.NET MVC错误HttpPost创建 - 传递模型POST之后Null值 - 模型绑定问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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