Inside a loop I'm trying to add an object with the type Location to a List<Location> property.
CONTROLLER:
[HttpPost]
public ActionResult Index([Bind(Include = "CitySearch")]HomeViewModel model)
{
List<CityIdentity> ids = null;
ids = service.GetNamesId(model.CitySearch);
foreach (var id in ids)
{
var loc = service.GetLocation(id.CityId);
var location = new Location
{
CityId = id.CityId,
Place = loc.Place,
};
model.Locations.Add(location);
}
}
VIEWMODEL:
public class HomeViewModel
{
public string CitySearch { get; set; }
public List<Location> Locations { get; set; }
}
model is of type HomeViewModel and property Locations is of type List<Location>. model is instance from form HttpPost action from submitting the form in the View.
I'm debugging and the error's occurs at model.Locations.Add(location) and I'm getting object reference not set to an instance of an object. and nullref exception.
Any idea?
Locationsprobably is null. Probably a problem with your markup that's causing the model binder to miss the type.modelassigned in the object?