开发者

ASP.NET MVC 2 Dropdown issue

I am using MVC 2 and entity framework 4. On my create application page I have a drop down list which I populated with values from my AccountType enum. This is how I did it:

public ActionResult Create()
      {
         // Get the account types from the account types enum
         var accountTypes = from AccountType at
                            in Enum.GetValues(typeof(AccountType))
                            select new
                            {
                               AccountTypeID = (int)Enum.Parse(typeof(AccountType), at.ToString()),
                               AccountTypeName = GetEnumFriendlyName(at)
                            };
         ViewData["AccountTypes"] = new SelectList(accountTypes, "AccountTypeID", "AccountTypeName");

         return View();
      }

This is what my code looks like for this drop down list data:

<%= Html.DropDownList("AccountTypeID", (SelectList)ViewData["AccountTypes"], "-- Select --") %>

After the page loads I start to enter some values. I select a value from the drop down list. When all the required input is entered I click on submit. Below is just a piece of the code:

[HttpPost]
      public ActionResult Create(Application application)
      {
         if (ModelState.IsValid)
         {
            application.ApplicationState = (int)State.Applying;
         }

         return View();
      }

Then I get the following error, not sure what it means, but I did Google it, tried the samples, but I still get the message. Here is the error message:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'AccountTypeID'.

I even changed the drop down list in the view to:

<%= Html.DropDownList("Accou开发者_如何学编程ntTypeID", (IEnumerable<SelectListItem>)ViewData["AccountTypes"], "-- Select --") %>

I'm not sure what I am doing wrong? I would appreciate some input :)

Thanks.


In your POST action you need to fill the ViewData["AccountTypes"] the same way you did in your GET action because you are returning the same view and this view depends on it:

[HttpPost]
public ActionResult Create(Application application)
{
    if (ModelState.IsValid)
    {
        application.ApplicationState = (int)State.Applying;
    }

    ViewData["AccountTypes"] = ... // same stuff as your GET action
    return View();
}

Obviously this answer comes with the usual disclaimer I always make when I see someone using ViewData instead of view models and strongly typed views: don't use ViewData, use view models and strongly typed views.


First: you can't have optional value to cast to Enum so you should receive in your Post a string and then make some logic to cast it to you enum:

    [HttpPost]
    public ActionResult Create(string application)
    {
        if (ModelState.IsValid)
        {
            // Do your stuff here to convert this string to your Enum 
            // But you should take care for null string
        }

        return View();
    }

Second: your DropDownList Id should be the same name of your Post action parameter's name: if you put

<%: Html.DropDownList("applicationID", (SelectList)ViewData["AccountTypes"], "-- Select --")%>

then your action should has "applicationID" parameter not "application"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜