绑定属性包括和复杂类型嵌套对象排除了物业 [英] bind attribute include and exclude property with complex type nested objects

查看:112
本文介绍了绑定属性包括和复杂类型嵌套对象排除了物业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是不可思议。我不能使用的 BindAttribute 包含 Exculude 与性能在ASP.NET MVC的复杂类型嵌套对象。

Ok, this is weird. I cannot use BindAttribute's Include and Exculude properties with complex type nested objects on ASP.NET MVC.

下面是我做过什么:

这是我的模型:

public class FooViewModel {

    public Enquiry Enquiry { get; set; }
}

public class Enquiry {

    public int EnquiryId { get; set; }
    public string Latitude { get; set; }
}

HTTP POST动作:

HTTP Post action:

[ActionName("Foo"), HttpPost]
public ActionResult Foo_post(
    [Bind(Include = "Enquiry.EnquiryId")]
    FooViewModel foo) {

    return View(foo);
}

查看:

@using (Html.BeginForm()) {

    @Html.TextBoxFor(m => m.Enquiry.EnquiryId)
    @Html.TextBoxFor(m => m.Enquiry.Latitude)

    <input type="submit" value="push" />
}

不工作的时候。如果我定义了 BindAttribute 查询类我只能做这项工作,因为它是在这里指出:

Does not work at all. Can I only make this work if I define the BindAttribute for Enquiry class as it is stated here:

<一个href=\"http://stackoverflow.com/questions/6739012/how-do-i-use-the-bindinclude-attribute-on-complex-nested-objects\">How做我使用[绑定(包括=&QUOT;&QUOT;)]在复杂的嵌套对象的属性

推荐答案

是的,你可以把它像工作:

Yes, you can make it work like that:

[Bind(Include = "EnquiryId")]
public class Enquiry 
{
    public int EnquiryId { get; set; }
    public string Latitude { get; set; }
}

和你的行动:

[ActionName("Foo"), HttpPost]
public ActionResult Foo_post(FooViewModel foo) 
{
    return View(foo);
}

这将只包括 EnquiryId 中的绑定和离开纬度空。

This will include only the EnquiryId in the binding and leave the Latitude null.

这是说,使用绑定属性不是东西,我会建议您。我的建议是使用视图模型。这些内部视图模型,你只包含了这期特别意义的属性。

This being said, using the Bind attribute is not something that I would recommend you. My recommendation is to use view models. Inside those view models you include only the properties that make sense for this particular view.

所以,简单地重新适应您的视图模型:

So simply readapt your view models:

public class FooViewModel 
{
    public EnquiryViewModel Enquiry { get; set; }
}

public class EnquiryViewModel 
{
    public int EnquiryId { get; set; }
}

有你走。不用再担心结合。

There you go. No longer need to worry about binding.

这篇关于绑定属性包括和复杂类型嵌套对象排除了物业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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