相关疑难解决方法(0)

数组的MVC3下拉列表

尝试从一组国家/地区加载下拉列表:

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>
Run Code Online (Sandbox Code Playgroud)

这是输出:

<option af?"selected="\&quot;selected\&quot;&quot;:&quot;&quot;)" (us="=" value="AF">Afghanistan</option>
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我该如何解决?我也尝试了这个,但得到一个例外:

@Html.DropDownList("BusinessCountry", new SelectList(Countries, "CountryCode", "CountryName", @ViewBag.part.BusinessCountry), Countries)
Run Code Online (Sandbox Code Playgroud)

已经弄清楚如何使用我的代码来做到这一点:

<select id="BusinessCountry" name="BusinessCountry" class="validate[required]" parentTab="tab4" style="width: 160px;">
@foreach(Country c in …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-3

2
推荐指数
1
解决办法
2846
查看次数

标签 统计

asp.net-mvc-3 ×1

c# ×1