2

Okay i'm new to asp mvc2 and i'm experiencing some problems with the htmlhelper called Html.dropdownlistfor();

I want to present the user a list of days in the week. And i want the selected item to be bound to my model.

I have created this little class to generate a list of days + a short notation which 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 really have now idea if this is the correct way to do it

Then i putted 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 view to display the list:

<div class="editor-field">
            <%: Html.DropDownListFor(model => model.ChosenDay, ViewData["days"] as SelectList, "--choose Day--")%>
        </div>

Now this all works perfect. On the first visit, But then when i'm doing a [HttpPost] Which looks like the following:

[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 thrown:

The ViewData item that has the key 'ChosenDay' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.

This errors gets thrown at the line in my view where i display the dropdown list.

I really have no idea how to solve this and tried several solutions i found online. but none of them really worked.

Ty in advance!

2 Answers 2

2

I have seen such an error. This is becouse ViewData["days"] as SelectList is null when the view is rendered. This can be becouse of ViewData["days"] is null or in different type then SelectList. The problem must 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"] not null and IEnumerable before you return View. It must be becouse of Model.IsValid so ViewData["days"] not binded.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey. now i notice! Thanks for your help :D
0

The HttpPost controller action will call the model constructor when it sees "EventRegistreerViewModel model".

[HttpPost] 
public ActionResult Registreer(EventRegistreerViewModel model) 

So if you add code to your EventRegistreerViewModel model 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 way every time the view model is constructed it will include the select list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.