Model Binding not working
asp.net-mvc, asp.net-mvc-3
Solution
Here's the problem:
[HttpPost]
public ActionResult Create(LocationViewModel location)
Do you see it? It's the name of your action argument: `location`.
Look at your view model now, it has a property named `Location`:
public Location Location { get; set; }
This confuses the model binder. It no longer knows whether you need to bind the `LocationViewModel` or its property.
So simply rename to avoid the conflict:
[HttpPost]
public ActionResult Create(LocationViewModel model)
Problem
I have the following POCO classes: ``` public class Location { public int LocationId { get; set; } public string Name { get; set; } public string Street { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public string Country { get; set; } public float? Latitude { get; set; } public float? Longitude { get; set; } public string PhoneNumber { get; set; } public string EmailAddress { get; set; } public string Website { get; set; } public virtual ICollection<Program> Programs { get; set; } public virtual ICollection<PlayerType> PlayerTypes { get; set; } } public class PlayerType { public int PlayerTypeId { get; set; } public string Name { get; set; } public int SortOrder { get; set; } public bool IsActive { get; set; } public virtual ICollection<Location> Locations { get; set; } } ``` And a View Model Class ``` public class LocationViewModel { public Location Location { get; set; } public IList<PlayerType> SelectPlayerTypes { get; set; } public LocationViewModel() { Location = new Location(); } } ``` Within my Create Form, I have defined the model as ``` @model Locator.Models.LocationViewModel ``` And have fields like the following: ``` div class="editor-label"> @Html.LabelFor(model => model.Location.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Location.Name) @Html.ValidationMessageFor(model => model.Location.Name) </div> ``` In my controller to handle the POST I have ``` [HttpPost] public ActionResult Create(LocationViewModel location) { if (ModelState.IsValid) { locationRepository.InsertOrUpdate(location.Location); locationRepository.Save(); return RedirectToAction("Index"); } location.SelectPlayerTypes = golferTypeRepository.All.Where(p => p.IsActive).ToList(); return View(location); } ``` The problem is that I have a Location object but none of the properties are to set to the values entered in the form. Am I doing something wrong here? Thanks