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

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

问题描述

我有一个带有 Foo 类的模型对象结构,该类包含一个带有字符串值的 Bar.

公共类Foo{公共酒吧;}公共课吧{公共字符串值 { 获取;放;}}

还有一个使用这种结构的视图模型

公共类 HomeModel{公共福福;}

然后我有一个表单,它在 Razor 中看起来像这样.

<div>@using (Html.BeginForm("Save", "Home", FormMethod.Post)){<字段集>@Html.TextBoxFor(m => m.Foo.Bar.Value)<输入类型=提交"值=发送"/></fieldset>}

在 html 中.

<字段集><input id="Foo_Bar_Value" name="Foo.Bar.Value" type="text" value="Test"><输入类型=提交"值=发送"></fieldset></表单>

终于有控制器来处理像这样的post loos

[HttpPost]公共 ActionResult 保存(Foo foo){//魔法在这里发生return RedirectToAction("索引");}

我的问题是 Foo 中的 Barnull 一旦它点击 Save 控制器操作(Foo 已创建,但带有 null Bar 字段.

我认为 MVC 中的模型绑定器将能够创建 FooBar 对象并设置 Value 属性,只要它看起来像上面那样.我错过了什么?

我也知道我的视图模型有点过于复杂并且可以更简单,但是对于我正在尝试做的事情,如果我可以使用更深层次的对象结构,我真的会帮助我.以上示例使用 ASP.NET 5.

解决方案

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

公共类 HomeModel{公共 Foo Foo { 得到;放;}}

其次,帮助程序基于 HomeModel 生成控件,但您回发回 Foo.要么将 POST 方法更改为

[HttpPost]公共 ActionResult 保存(HomeModel 模型)

或使用 BindAttribute 来指定 Prefix(它本质上从发布的值中剥离了前缀的值 - 所以 Foo.Bar.Value 成为 Bar.Value 用于绑定)

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

另请注意,您不应该使用与您的属性之一相同的名称命名方法参数,否则绑定将失败并且您的模型将为空.

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>

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).

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?

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.

解决方案

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

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

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)

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天全站免登陆