MVC3 - 为什么导航性能空回发 [英] MVC3 - Why are navigation properties null on postback

查看:121
本文介绍了MVC3 - 为什么导航性能空回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何有导航属性对我的模型(EF4)我发布后,不能为空。我有几个基本的网页同样的问题。

I can't figure out how to have navigation properties for my model (EF4) not be null after I post. I have the same problem on several basic pages.

即。我有一个表
估计
与外键
为LoginID(从登录表)。

i.e. I have a table Estimate with a foreign key of LoginID (from the Login table).

在我HTTPGET编辑方法,我觉得具体估计我想:

In my HTTPGet Edit method, I find the specific estimate I want:

Estimate estimate = db.Estimates.Find(id)

在这里,我可以访问登录导航属性和任何从登录表中记录的属性。

From here I can access the Login navigation property and any of the properties of that record from the Login table.

我通过这个估计的模型,我的强类型视图。当我HTTPPost编辑方法被调用(和模型传递给它作为一个参数),我登录导航属性为空,我在那里不能访问任何东西。我可以避开它当然是因为我有我的估计类为LoginID字段,但是那感觉真是笨重,像我错过了实体框架的一个关键优势。

I pass this estimate as the model for my strongly typed view. When my HTTPPost Edit method gets called (and the model is passed to it as a parameter), my Login navigation property is null and I can't access anything in it. I can get around it of course because I do have the LoginID field in my estimate class, but it feels really clunky and like I'm missing out on a key benefit of the entity framework.

我在哪里,我传递一个模型导航属性我的看法和观点与返回一个空导航属性模型的多个页面同样的问题。

I have this same problem on multiple pages where I'm passing a model with a navigation property to my view and the view is returning the model with a null navigation property.

下面是我遇到的麻烦的code的例子:

Below is an example of the code I was having trouble with:

    [HttpPost]
    public ActionResult Edit(Estimate estimate)
    {

        var test = estimate.Login.CompanyProfileID;
        ...

我可以访问Model.Login和它的所有性能只是认为很好,所以我知道这是获得通过的观点正确。它只是当我的表单提交不会在导航属性传回给控制器。

I can access Model.Login and all of it's properties just fine in the view, so I know it's getting passed to the view properly. It just doesn't pass the navigation property back to the controller when my form submits.

推荐答案

正在发生的事情是,MVC使用一些所谓的模型绑定,这所有的字段匹配POST通过在页面请求的参数匹配属性你的行动。

What is happening is that MVC uses something called 'Model Binding', which matches all the fields that POST passes in the page request to matching properties of the parameter of your action.

所以,如果在 POST 页输入字段不包括财产,它不会被绑定到你的 POST参数的行动,因此将是无效。

So, if the property is not included in the input fields in the POST page, it will not be bound to the parameter in your POST action, and thus, will be null.

所以,你既可以包括登录,例如为每个属性的隐藏字段

So, you could either include a hidden field for every property in Login, e.g.

@Html.HiddenFor(m => m.Login.ID)
@Html.HiddenFor(m => m.Login.Name)

或者,只是做你在你的解决办法形容 - 即重新查询基于ID的数据库。虽然这看起来笨重的,这是一个做事的非常有效的方法,因为它避免了所有的地方来回传递隐藏字段。

Or, just do as you describe in your workaround - ie requery the database based on the Id. Although this may seem clunky, this is a perfectly valid way of doing things, as it avoids having to pass hidden fields around all over the place.

这篇关于MVC3 - 为什么导航性能空回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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