private SelectList getVenueSelectList(Guid venueid)
{
var vVar = from v
in DataContext.Venues orderby v.Name
select v;
List<Venue> vList = vVar.ToList<Venue>();
vList.Insert(0, new Venue());
vList[0].Name = "<None>";
vList[0].VenueID = Guid.Empty;
return new
SelectList(vList, "VenueID",
"Name", venueid);
}
The view has this:
Html.DropDownList("VenueID", ViewData["VenueID"]
as SelectList)
When I view an existing items I expect to see the appropriate item in the list selected, but it's not. So, as with the beta, I still have to use the following snippet of startup script:
If the SelectList is stored in ViewData using the same key name as what you're using for your "name", then there is no need to manually extract the SelectList from ViewData. In other words, you can just say this in your view:
Html.DropDownList("VenueID")
This should preserve any selected items that you had in the SelectList. I think there might very well have been a bug in the DropDownList code, but we felt that since there's a simple workaround we could postpone the bug fix.
jonesie
Member
6 Points
21 Posts
MVP
RC2 & DropDownList 's
Mar 08, 2009 10:00 PM|LINK
I'm upgrading a beta project to RC2. DropDownList... sigh.
My controller code to create a SelectList looks like this:
ViewData["VenueID"] = getVenueSelectList(aValidGuidHere);
private SelectList getVenueSelectList(Guid venueid)
{
var vVar = from v in DataContext.Venues orderby v.Name select v;
List<Venue> vList = vVar.ToList<Venue>();
vList.Insert(0, new Venue());
vList[0].Name = "<None>";
vList[0].VenueID = Guid.Empty;
return new SelectList(vList, "VenueID", "Name", venueid);
}
The view has this:
Html.DropDownList("VenueID", ViewData["VenueID"] as SelectList)
When I view an existing items I expect to see the appropriate item in the list selected, but it's not. So, as with the beta, I still have to use the following snippet of startup script:
$(function() {
$("select[@name='VenueID'] option[@value='<%= ViewData.Model.VenueID %>']").attr('selected', 'selected');
});
Any I missing something here or is this still not fixed?
Thanks
New Zealand
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: RC2 & DropDownList 's
Mar 10, 2009 04:42 PM|LINK
Hi there,
If the SelectList is stored in ViewData using the same key name as what you're using for your "name", then there is no need to manually extract the SelectList from ViewData. In other words, you can just say this in your view:
Html.DropDownList("VenueID")
This should preserve any selected items that you had in the SelectList. I think there might very well have been a bug in the DropDownList code, but we felt that since there's a simple workaround we could postpone the bug fix.
Thanks,
Eilon