阵列中的MVC3下拉列表 - c#

尝试从多个国家/地区加载下拉列表:

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>

参考方案

在视图中混合很多代码是错误的方法。还使用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>();
  }
}

现在,在您的GET Action方法中,您将数据填充到viewModel对象的Countries集合中并将其发送到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方法中,您将通过访问发布的模型的SelectecCountry属性值来获取选定的国家/地区ID。

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

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…

jQuery Ajax文件上传在客户端浏览器上无法正常工作 - javascript

我正在尝试使用Ajax和JQuery实现个人资料图片上传功能我能够将个人资料图片成功上传到我尝试过的所有机器和移动设备上的数据库中。它适用于我在Chrome,Edge,Firefox,Safari甚至Vivaldi上使用。问题是我住在加拿大的客户无法将他们的个人资料图片上传到数据库。另外,纵向宽高比的图像倾向于向侧面旋转。我一直在要求他重​​新注册并多次提供…

将python scikit学习模型导出到pmml - python

我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…