Reputation: 301
I am developing a web app using ASP.NET MVC, I have two entities like this:
[DataContract]
public class Entity1 : IEntity<int>
{
[DataMember(Name="id")]
public int Id {get; set;}
[DataMember(Name="name")]
public string Name {get; set;}
[DataMember(Name="list")]
public ICollection Entity2Collection {get; set;}
}
[DataContract]
public class Entity2 : IEntity<int>
{
[DataMember(Name="id")]
public int Id {get; set;}
[DataMember(Name="name")]
public string Name {get; set;}
[DataMember(Name="entity3id")]
public int Entity3ObjId {get; set;}
public Entity3 Entity3Obj {get; set;}
}
And in the controller action I have the following:
[HttpPost]
public ActionResult Edit(Entity1 entity)
{
if(ModelState.IsValid){
await repository.updateAsync(entity);
}
return View();
}
In the entity parameter, the property Entity2Collection is always null, I don't know what I am missing because I have this in the view
@Html.HiddenFor(model => model.Entity2Collection)
But it always null when I do a Post request. I hope for a little help.
Upvotes: 0
Views: 2265
Reputation: 3505
The name of the parameter in the controller has to be the same as the name of the field, so you must name your field entity if that is what is in the controller
Also look at the following blog which shows how model binding with complex objects works in MVC
http://blog.codeinside.eu/2012/09/17/modelbinding-with-complex-objects-in-asp-net-mvc/
Upvotes: 1