3

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

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="\&quot;selected\&quot;&quot;:&quot;&quot;)" (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>
4

3 回答 3

6

在视图中混合大量代码是一种错误的做法。还使用ViewBag/ViewData在操作方法和视图之间传输这样的数据,使您的代码难看。您应该考虑使用 ViewModel 将数据从操作方法传输到视图。

假设您的视图是创建公司详细信息,则具有这样的视图模型

public class CompanyViewModel
{
  public string Name { set;get;}
  public IEnumerable<SelectListItem> Countries { set;get;}
  public int SelectedCountry { set;get;}

  CompanyViewModel()
  {
    Countries=new List<SelectListItem>();
  }
}

现在在您的GETAction 方法中,您将数据填充到CountriesviewModel 对象的集合中并将其发送到 View。

public ActionResult Create()
{
   CompanyViewModel vm=new CompanyViewModel();
   // The below line is hard coded for demo. you may replace 
   //  this with loading data from your Data access layer/ Existing array
   vm.Countries= new[]
   {
      new SelectListItem { Value = "1", Text = "United States" },
      new SelectListItem { Value = "2", Text = "Canada" },
      new SelectListItem { Value = "3", Text = "Australia" }
   };
   return View(vm);
}

现在在您的强类型视图中,

@model CompanyViewModel
@using(Html.Beginform())
{
   @Html.DropDownListFor(x => x.SelectedCountry,
                   new SelectList(Model.Countries,"Value","Text"), "Select..")
   <input type="submit" />

}

现在在您的方法中,您将通过访问发布的模型的属性值来HTTPPost获取选定的国家/地区 IDSelectecCountry

[HttpPost]
public ActionResult Create(CompanyViewModel model)
{
  if(ModelState.IsValid)
  {
      //check for model.SelectedCountry property value here
      //Save and Redirect
  }
  //Reload countries here
  return View(model);
}
于 2012-09-12T19:09:08.680 回答
0

我使用这样的下拉列表:

在控制器中:

ViewBag.CompanyId = New SelectList(db.Companies, "CompanyId", "Name", blog.CompanyId)

在视图中:

<div class="editor-field">
    @Html.DropDownList("CompanyId", String.Empty)
    @Html.ValidationMessageFor(Function(model) model.CompanyId)
</div>

注意:这是 VB。

于 2012-09-12T18:45:59.597 回答
0

试试这个代码的属性

@((ViewBag.BusinessCountry == @c.CountryCode) ? "selected='selected'" : "")
于 2012-09-12T18:49:41.107 回答