-1

爲什麼foreach在擴展方法中返回一行?這些代碼在正常方法內正常運行。返回一列可能是什麼原因?爲什麼foreach在擴展方法裏返回一行?

public static MvcHtmlString DropDownWithOpt(this HtmlHelper html) 
{ 
    IList<Countries> c = new List<Countries> 
    { 
     new Countries{ Country = "Georgia", Cities = new List<Cities> 
     { 
     new Cities{ City="Batumi"}, 
     new Cities{ City="Tbilisi"}, 
     new Cities{ City="Zugdidi"} 
     }}, 
     new Countries{ Country = "Turkey", Cities = new List<Cities> 
     { 
     new Cities{ City="Istanbul"}, 
     new Cities{ City="Ankara"} 
     }} 
    }; 

    var select = new TagBuilder("select"); 
    var group = new TagBuilder("optgroup"); 
    var option = new TagBuilder("option"); 

    foreach (var item in c) 
    { 

     group.Attributes["label"] = item.Country; 
     foreach (var ci in item.Cities) 
     { 
      option.Attributes["value"] = ci.City.ToString(); 
      option.SetInnerText(ci.City); 
     } 
    } 
    select.InnerHtml = group.ToString(TagRenderMode.SelfClosing) + option.ToString(); 
    return MvcHtmlString.Create(select.ToString(TagRenderMode.Normal)); 
} 

回答

0

由於環路內要覆蓋它

您可以使用

Attributes.Add (key, value) 
相關問題