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であり、プロパティLocationsは typeList<Location>です。ビューでフォームを送信するフォーム アクションのmodelインスタンスです。HttpPost
デバッグ中にエラーが発生しmodel.Locations.Add(location)、object reference not set to an instance of an object.nullref 例外が発生しています。
何か案が?