My Controller populates my model with the list stringsthat appears DropDownListin my view. When the view is sent back to my controller, this list is suddenly null . Why is it null, and what happened to the list of strings I created?
The list was correctly populated and displayed in the view. The remaining elements of the form must correctly return back. For example, it selectedNamehas any name that the user clicked on. The only thing that doesn't go back is this nameList.
Here is the relevant part of my model,
public class MyModel
{
[Display(Name = "Selected")]
public string selectedName{ get; set; }
[Display(Name = "Names")]
public List<string> nameList{ get; set; }
}
the corresponding Get and Post points of my controller,
public class MyController: Controller
{
[HttpGet]
public ActionResult Index()
{
List<string> nameList= getNames();
MyModel model = new MyModel()
model.nameList= nameList;
return View(model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
if(model.nameList== null)
{
cry();
postOnStackOverflow();
}
return View(model);
}
}
and the corresponding part of my submission (which is enclosed inside the form).
<p>
@Html.LabelFor(c => c.nameList):
@Html.DropDownListFor(c => c.selectedName, new SelectList(Model.nameList), new { onchange = "this.form.submit();" })
</p>