asp.net MVC中Bind特性的使用

原文地址:http://stackoverflow.com/questions/8892550/bind-attribute-include-and-exclude-property-with-complex-type-nested-objects


This is my model:

public class FooViewModel {

    public Enquiry Enquiry { get; set; }
}

public class Enquiry {

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

HTTP Post action:

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

    return View(foo);
}

View:

@using (Html.BeginForm()) {

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

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

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

上边是用在Action操作方法中的参数中使用,

或者在Model类上使用

[Bind(Include="EnquiryId ")]
public class Enquiry {

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


你可能感兴趣的:(MVC)