尝试从国家数组加载下拉列表:
```html
这是输出结果:
我做错了什么,应该如何修复? 我也尝试过这个,但是会出现异常:
```html
尝试从国家数组加载下拉列表:
```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>