2012-04-18 5 views
0

ASP.NET MVC에서 채우기 기능을 만듭니다. 그것은 완벽 국가의 목록을 표시하지만 문제는 내가 결코 편집 페이지에서 저장된 값을 선택 없다는 것입니다ASP.NET MVC3 Dropdownlist는 편집보기에서 값을 선택하지 않습니다.

@Html.countryDropDown("Country"," ", ViewData["Country"]) 

:로

public static MvcHtmlString countryDropDown(this HtmlHelper helper, string name, string optionLabel, object selectedValue) 
    { 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/countries.xml")); 

     StringBuilder b = new StringBuilder(); 

     b.Append(string.Format("<select name=\"{0}\" id=\"{0}\">", name)); 
     if (!string.IsNullOrEmpty(optionLabel)) 
      b.Append(string.Format("<option value=\"\">{0}</option>", optionLabel)); 

     foreach (XmlNode node in doc.SelectNodes("//country")) 
     { 
      string selected = string.Empty; 
      if (node.Attributes["name"].Value == selectedValue as string) 
      { 
       selected = "selected=\"selected\""; 
      } 
      b.Append(string.Format("<option value=\"{0}\" {2}>{1}</option>", node.Attributes["name"].Value, node.Attributes["name"].Value, selected)); 
     } 
     b.Append("</select>"); 

     return MvcHtmlString.Create(b.ToString()); 
    } 

나는이 기능에서 생성 사용 및 편집 전망. 편집 페이지에서 값을 선택할 수 있도록 코드를 수정하는 방법.

+1

당신이 당신의 경우 실패 이유를 디버거로 코드를 단계별로 봤어의

@Html.countryDropDown("Country"," ", ViewData.Eval("Country")) 

? –

+0

그것은 selectedValue 매개 변수에 널 값을 가져옵니다. 즉 ViewData [ '국가']가 null을 보냈습니다. – hotcoder

+0

그러면 문제가 될 수 있습니다. –

답변

0

사용하여 해결하는 대신

@Html.countryDropDown("Country"," ", ViewData["Country"] 
관련 문제