从阵列MVC3下拉列表(MVC3 dropdown list from array)

2019-09-22 09:06发布

试图从国家的阵列加载下拉列表:

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>

Answer 1:

在视图中混合了大量的代码是一个错误的方式来做到这一点。 另外的使用ViewBag / ViewData传递作用的方法和观点之间这样的数据,让你的代码丑陋。 你应该考虑一个视图模型将数据从操作方法转移到查看。

假设你的看法是建立一个公司信息,有这样的视图模型

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

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

现在,在您的GET操作方法,你会填充数据的Countries视图模型对象的收集和发送到视图。

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方法,你将通过访问获得所选择的国家ID SelectecCountry发布模型的属性值

[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);
}


Answer 2:

我使用的下拉列表如下:

在控制器:

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。



Answer 3:

尝试使用属性验证码

@((ViewBag.BusinessCountry == @c.CountryCode) ? "selected='selected'" : "")


文章来源: MVC3 dropdown list from array