ASP.Net MVC Object Reference in Edit View when using DropDownListFor()

Posted by hermiod on Stack Overflow See other posts from Stack Overflow or by hermiod
Published on 2010-05-02T21:34:00Z Indexed on 2010/05/02 21:37 UTC
Read the original article Hit count: 497

This question is related to another I ask recently, it can be found here for some background information.

Here is the code in the Edit ActionResult:

    public virtual ActionResult Edit(int id)
    {
        ///Set data for DropDownLists.
        ViewData["MethodList"] = tr.ListMethods();
        ViewData["GenderList"] = tr.ListGenders();
        ViewData["FocusAreaList"] = tr.ListFocusAreas();
        ViewData["SiteList"] = tr.ListSites();
        ViewData["TypeList"] = tr.ListTalkbackTypes();
        ViewData["CategoryList"] = tr.ListCategories();

        return View(tr.GetTalkback(id));
    }

I add lists to the ViewData to use in the dropdownlists, these are all IEnumerable and are all returning values.

GetTalkback() returns an Entity framework object of type Talkback which is generated from the Talkback table.

The DropDownListFor code is:

<%: Html.DropDownListFor(model=>model.method_id,new SelectList(ViewData["MethodList"] as IEnumerable<SelectListItem>,"Value","Text",Model.method_id)) %>

The record I am viewing has values in all fields. When I click submit on the View, I get an Object reference not set to an instance of an object. error on the above line.

There are a number of standard fields in the form prior to this, so the error is only occurring on dropdown lists, and it is occurring on all of them.

Any ideas? This is my first foray in to MVC, C#, and Entity so I am completely lost!

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about c#