ASP.Net MVC2 DropDownListFor

Posted by hermiod on Stack Overflow See other posts from Stack Overflow or by hermiod
Published on 2010-05-01T12:50:33Z Indexed on 2010/05/01 13:07 UTC
Read the original article Hit count: 1339

Filed under:
|

Hi all

I am trying to learn MVC2, C# and Linq to Entities all in one project (yes, I am mad) and I am experiencing some problems with DropDownListFor and passing the SelectList to it.

This is the code in my controller:

        public ActionResult Create()
    {
        var Methods = te.Methods.Select(a => a);

        List<SelectListItem> MethodList = new List<SelectListItem>();

        foreach (Method me in Methods)
        { 
            SelectListItem sli=new SelectListItem();
            sli.Text = me.Description;
            sli.Value = me.method_id.ToString();
            MethodList.Add(sli);
        }


        ViewData["MethodList"] = MethodList.AsEnumerable();

        Talkback tb = new Talkback();
        return View(tb);
    } 

and I am having troubles trying to get the DropDownListFor to take the MethodList in ViewData. When I try:

<%:Html.DropDownListFor(model => model.method_id,new SelectList("MethodList","method_id","Description",Model.method_id)) %>

It errors out with the following message DataBinding: 'System.Char' does not contain a property with the name 'method_id'.

I know why this is, as it is taking MethodList as a string, but I can't figure out how to get it to take the SelectList. If I do the following with a normal DropDownList:

<%: Html.DropDownList("MethodList") %>

It is quite happy with this.

Can anyone help?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about c#