尝试从一系列国家/地区加载下拉列表:
Country[] Countries = ViewBag.mps.GetCountryList(ViewBag.LogonTicket, ViewBag.PID);
/* Country object defined as, returned from WCF webservice call above:
<xs:complexType name="Country">
<xs:sequence>
<xs:element minOccurs="0" name="CountryName" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="CountryCode" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
*/
<select id="BusinessCountry" name="BusinessCountry" class="validate[required]" parentTab="tab4" style="width:160px;">
@{
foreach(Country c in Countries) {
<option value="@c.CountryCode" (@ViewBag.BusinessCountry == @c.CountryCode?"selected=\"selected\"":"") >@c.CountryName</option>
}
}
</select>
这是输出:
<option af?"selected="\"selected\"":"")" (us="=" value="AF">Afghanistan</option>
我做错了什么,我该如何解决?我也试过这个但得到一个例外:
@Html.DropDownList("BusinessCountry", new SelectList(Countries, "CountryCode", "CountryName", @ViewBag.part.BusinessCountry), Countries)
已经想出了如何使用我拥有的代码来做到这一点:
<select id="BusinessCountry" name="BusinessCountry" class="validate[required]" parentTab="tab4" style="width: 160px;">
@foreach(Country c in Countries) {
string sel = (ViewBag.part.BusinessCountry == c.CountryCode?"selected=\"selected\"":"");
<option value="@c.CountryCode" @sel >@c.CountryName</option>
}
</select>