ASP MVC 2: error with dropdown in POST
Ok i am new to asp mvc2 and i am having some problems with htmlhelper called Html.dropdownlistfor ();
I want to present the user with a list of days of the week. And I want the selected item to snap to my model.
I created this little class to generate a list of days + a short notation that I will use to store it in the database.
public static class days
{
public static List<Day> getDayList()
{
List<Day> daylist = new List<Day>();
daylist.Add(new Day("Monday", "MO"));
daylist.Add(new Day("Tuesday", "TU"));
// I left the other days out
return daylist;
}
public class Dag{
public string DayName{ get; set; }
public string DayShortName { get; set; }
public Dag(string name, string shortname)
{
this.DayName= name;
this.DayShortName = shortname;
}
}
}
I actually have an idea if this is the correct way to do it
Then I put this in my controller:
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View("");
I have this line in my model
public string ChosenDay { get; set; }
And this, in my opinion, is for displaying the list:
<div class="editor-field">
<%: Html.DropDownListFor(model => model.ChosenDay, ViewData["days"] as SelectList, "--choose Day--")%>
</div>
Everything works fine now. On first visit, But then when I do [HttpPost] It looks like this:
[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
// I removed some unrelated code here
// The code below executes when modelstate.isvalid == false
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View(model);
}
Then I will have the following exception:
The ViewData item that has the key 'ChosenDay' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
These errors end up on a line in my view where I show the dropdown.
I really don't know how to solve this and tried several solutions I found online. but none of them worked.
Ty in advance!
I've seen this error. This is because it ViewData["days"] as SelectList
is null when the view is rendered. It could be because ViewData ["days"] is null or in a different type and then SelectList. The problem should be found here:
[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
}
Make shure that this code
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
runs and that ViewData ["days"] is not null and IEnumerable before returning the View. This must be from Model.IsValid, so ViewData ["days"] is not bound.
a source to share
The HttpPost controller action will call the model constructor when it sees the "EventRegistreerViewModel".
[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
So if you add code to your EventRegistreerViewModel like this:
...
public IEnumerable<string> MySelectList { get; set; }
public EventRegistreerViewModel() {
// build the select the list as selectList, then
this.MySelectList = selectList;
}
Then in your view, use this code to display the list and the selected item:
Html.DropDownListFor(model => model.ChosenDay, model.MySelectList, "--choose Day--")
This method will include a selection list every time a view model is created
a source to share