Ala
Ala

Reputation: 1503

Bind Exclude not working for child objects

I have the same problem as this question and tried the suggested solutions but no luck.. Any help will be very appreciated. Bind Exclude not working Model Binding for child objects in ASP.Net MVC

The problem is as following: I got into a issue in Model Binding in Asp.Net MVC. I have view model like below,

public class ArticleViewModel : BaseViewModel
    {        
        public Article art { get; set; }
        public List<ArticleAttachment> attachments { get; set; }
    }

I am trying to exclude model binding a property on the "Article" child object as seen below in my action method,

[HttpPost]
[ValidateInput(false)]
public ActionResult New([Bind(Exclude = "art.Abstract")]ArticleViewModel articleVM)
 {

But the model binder populates the property called Abstract even with the above setting.

Upvotes: 0

Views: 866

Answers (1)

user3559349
user3559349

Reputation:

To make this work, you need to apply the [Bind] attribute the ArticleAttachment class.

[Bind(Exclude="Abstract")]
public class ArticleAttachment
{
  public string Abstract { get; set; }
  ....
}

However, any time you use either the [Bind(Include="..")] or [Bind(Exclude="..")] attributes, delete them and do it properly using a view model to represent what you want to edit.

Upvotes: 1

Related Questions