Thursday, February 17, 2011

Save state for DropDownList after post

Hi, i have form with one textbox and dropdownlist

<%=Html.TextBox("Name")%>
<%=Html.ValidationMessage("Name", "Enter name")%>

<%=Html.DropDownList("Not Selected", "DomainID", ViewData["DomainID"])%>


I select value from drop downlist but dont fill name, im getting validation for textbox and then i need select value from dropdown again, of course with textbox it works but what can i do with list??

From stackoverflow
  • I assume that ViewData["DomainID"] is a SelectList? The constructor for SelectList takes a selectedValue object. Use that to pass the selected value in to your select list in the post action.

    msony : Did but still dont want select: ViewData["DomainID"] = new SelectList(commonService.GetDomains(), "ID", "Name", (item.Domain == null ? 0 : item.Domain.ID));
    Tim Scott : Maybe item.Domain is null?

0 comments:

Post a Comment