在循环中,我试图将具有该类型的对象添加Location
到List<Location>
属性中。
控制器:
[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);
}
}
视图模型:
public class HomeViewModel
{
public string CitySearch { get; set; }
public List<Location> Locations { get; set; }
}
model
是 typeHomeViewModel
和 propertyLocations
是 type List<Location>
。是来自在视图中提交表单的model
表单操作的实例。HttpPost
我正在调试并且错误发生在model.Locations.Add(location)
并且我得到了object reference not set to an instance of an object.
nullref 异常。
任何的想法?