Have just started playing with ASP.NET MVC and have stumbled over the following situation. It feels a lot like a bug but if its not, an explanation would be appreciated :)
The View contains pretty basic stuff
<%=Html.DropDownList("MyList", ViewData["MyListItems"] as SelectList)%>
<%=Html.TextBox("MyTextBox")%>
When not using a model, the value and selected item are set as expected:
//works fine
public ActionResult MyAction(){
ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); //items is an ienumerable of {Value="XXX", Text="YYY"}
ViewData["MyList"] = "XXX"; //set the selected item to be the one with value 'XXX'
ViewData["MyTextBox"] = "ABC"; //sets textbox value to 'ABC'
return View();
}
But when trying to load via a model, the textbox has the value set as expected, but the dropdown doesnt get a selected item set.
//doesnt work
public ActionResult MyAction(){
ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); //items is an ienumerable of {Value="XXX", Text="YYY"}
var model = new {
MyList = "XXX", //set the selected item to be the one with value 'XXX'
MyTextBox = "ABC" //sets textbox value to 'ABC'
}
return View(model);
}
Any ideas? My current thoughts on it are that perhaps when using a model, we're restricted to setting the selected item on the SelectList constructor instead of using the viewdata (which works fine) and passing the selectlist in with the model - which would have the benefit of cleaning the code up a little - I'm just wondering why this method doesnt work....
Many thanks for any suggestions
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) %>
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.
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))
%>
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)
来源:https://stackoverflow.com/questions/390083/asp-net-mvc-html-dropdownlist-value-not-set-via-viewdata-model