复杂的对象和模型绑定ASP.NET MVC [英] Complex object and model binder ASP.NET MVC

查看:121
本文介绍了复杂的对象和模型绑定ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含酒吧一个字符串值类的模型对象结构。

I have a model object structure with a Foo class that contains a Bar with a string value.

public class Foo
{
    public Bar Bar;
}

public class Bar
{
    public string Value { get; set; }
}

和使用这样的结构视图模型

And a view model that uses that structure like this

public class HomeModel
{
    public Foo Foo;
}

然后我有鉴于形式,在剃刀看起来是这样的。

I then have a form in view that in Razor looks something like this.

<body>
    <div>
        @using (Html.BeginForm("Save", "Home", FormMethod.Post))
        {
            <fieldset>
                @Html.TextBoxFor(m => m.Foo.Bar.Value)
                <input type="submit" value="Send"/>
            </fieldset>
        }

    </div>
</body>

HTML中变得

In html that becomes.

<form action="/Home/Save" method="post">
    <fieldset>
        <input id="Foo_Bar_Value" name="Foo.Bar.Value" type="text" value="Test">
        <input type="submit" value="Send">
    </fieldset>
</form>

最后,控制器处理后洛斯这样

Finally the controller to handle the post loos like this

[HttpPost]
public ActionResult Save(Foo foo)
{
    // Magic happends here
    return RedirectToAction("Index");
}

我的问题是,酒吧一旦它击中保存控制器动作(创建但是有一个 酒吧字段)。

My problem is that Bar in Foo is null once it hits the Save controller action (Foo is created but with an null Bar field).

我想在MVC模型绑定将能够创建酒吧对象,并设置属性,只要它看起来像上面。我缺少什么?

I thought the model binder in MVC would be able to create the Foo and the Bar object and set the Value property as long as it looks like the above. What am I missing?

我也知道我的看法模式是有点过分复杂,可​​能是简单,但我为我想做我真的帮助我,如果我可以用更深层次的对象结构。上面的例子使用ASP.NET 5。

I also know my view model is a bit over complicated and could be simpler but I for what I'm trying to do I'd really help me if I could use the deeper object structure. The examples above uses ASP.NET 5.

推荐答案

首先, DefaultModelBinder ,所以你需要使用属性将不会绑定到域

Firstly, the DefaultModelBinder will not bind to fields so you need to use properties

public class HomeModel
{
  public Foo Foo { get; set; }
}

其次,佣工基础上产生 HomeModel 控制,但你张贴回。要么改变POST方法

Secondly, the helpers are generating controls based on HomeModel but you posting back to Foo. Either change the POST method to

[HttpPost]
public ActionResult Save(HomeModel model)

BindAttribute 来指定 preFIX (基本上去掉$ P $的价值PFIX从发布的价值观 - 让 Foo.Bar.Value 变成 Bar.Value 的结合的目的)

or use the BindAttribute to specify the Prefix (which essentially strips the value of prefix from the posted values - so Foo.Bar.Value becomes Bar.Value for the purposes of binding)

[HttpPost]
public ActionResult Save([Bind(Prefix="Foo")]Foo model)

另外请注意,你不应该命名同名的方法的参数作为属性之一,否则绑定将失败,你的模型将是空的。

Note also that you should not name the method parameter with the same name as one of your properties otherwise binding will fail and your model will be null.

这篇关于复杂的对象和模型绑定ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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