ASP.NET MVC - Html.DropDownList - Value not set via ViewData.Model

泪湿孤枕 提交于 2019-11-28 20:36:50

After a bunch of hemming and hawing it boils down to the following line of code

if (ViewData.ModelState.TryGetValue(key, out modelState))

which means MVC is trying to resolve the value by only looking at the ViewData Dictionary<> object and not traversing down into the ViewData.Model object.

Whether that's a bug, limitation or design decision I'm not sure. However, you can fix it the following way:

<%= Html.TextBox("MyTextBox", ViewData.Model.MyTextBox) %>
stun

Actually, you just have to pass in null for the Html.DropDownList().
I was having the same exact problem, and used the Reflector to look at the MVC Source Code.

In the System.Web.Mvc.Extensions.SelectExtensions class's SelectInternal() method, it checks whether the selectList parameter is null or not. If it is passed in as null, it looks up the SelectList properly.

Here is the "Code-behind".

ViewData["MyDropDown"] = new SelectList(selectListItems,
                             "Value",
                             "Text",
                             selectedValue.ToString()
                         );

Here is the HTML view code.

<%= Html.DropDownList("MyDropDown", null,
        "** Please Select **",
        new { @class = "my-select-css-class" }
    ) %>

Note: I'm using ASP.NET MVC 2.0 (Beta Version).

UPDATE NOTE: January 31st 2012

After extensively using ASP.NET MVC for the past 3 years, I prefer using additionalViewData from the Html.EditorFor() method more.

Pass in your [List Items] as an anonymous object with the same property name as the Model's property into the Html.EditorFor() method.

<%= Html.EditorFor(
    m => m.MyPropertyName,
    new { MyPropertyName = Model.ListItemsForMyPropertyName }
) %>

If you want more details, please refer to my answer in another thread here.

try setting the selected value in the controller action when creating the SelectList collection.

ViewData["AddressTypeId"] = new SelectList(CustomerService.AddressType_List(), "AddressTypeId", "Name", myItem.AddressTypeId);

I'm obviously late here. But I wanted to add this comment incase someone else came across this issue.

In MVC 2. You can do the following to select the item...

public ActionResult Edit(int id)
        {
            Team team = _db.TeamSet.First(m => m.TeamID == id);
            var fanYear = from c in _db.FanYearSet select c;
            ViewData["FantasyYear"] = new SelectList(fanYear, "YearID", "FantasyYear", team.FanYearReference.EntityKey.EntityKeyValues[0].Value);
            var league = from c in _db.LeagueSet select c;
            ViewData["League"] = new SelectList(league, "LeagueID", "League_Name", team.LeaguesReference.EntityKey.EntityKeyValues[0].Value);

            return View(team);
        }

The fourth parameter takes a value and will select the value you want in the dropdownlist. In my example I have a table called team and it is I have a relationship set to a table called fanYear and league.

You can see that I first get the team that I'm editing, then building a dropdownlist of fantasy year and another for leagues. In order to determine the year and league for the team, I had to use Entity Reference.

Just wanted to put that out there. I couldn't find any examples of doing this.

When you have problems with invalid or null ViewData, pay attention to your controller's action.

Suppose you have a ViewData called MyList for Html.DropDownlist called MyList.

If you call ModelState.AddModelError("MyList","Please select the profession") you'll replace your ViewData list content with the this warning text.

That's also why people around internet is having null problems with ViewData on DropDownList.

The bottom line is, pay attention to your IDs on error model, they must not interfere with your html controls.

user448862

My concern with this is that you may only use the list for one field. I have Entities with multiple user fields, so I use the following:

< %= Html.DropDownList("fieldAAA", 
                       new SelectList( (IEnumerable) ViewData["Users"], "Value", "Text", fieldAAA))

%> 
Aracnid

Ok I may be missing something from this thread however in C# the following works:

Html.DropDownList("ProfessionGuid", (SelectList)ViewData["Professions"])

Where 'ProfessionGuid' is the value to be selected in the list.

In VB.Net I believe it would be:

Html.DropDownList("ProfessionGuid", ViewData["Professions"] AS SelectList)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!