问题描述
试图从国家的数组加载一个DropDownList:
国家[]国家= ViewBag.mps.GetCountryList(ViewBag.LogonTicket,ViewBag.PID);
定义为/ *国家目标,从上面WCF Web服务调用返回:
< XS:复杂类型名称=国家>
< XS:序列>
< XS:元素的minOccurs =0NAME =国家或地区名称的nillable =真类型=XS:字符串/>
< XS:元素的minOccurs =0NAME =国家code的nillable =真类型=XS:字符串/>
< / XS:序列>
< / XS:复杂类型>
* /
<选择一个id =BusinessCountryNAME =BusinessCountry级=验证[需要]parentTab =TAB4的风格=宽度:160像素;>
@ {
的foreach(C国的国家){
<期权价值=@ c.Country code(@ ViewBag.BusinessCountry == @ c.Country code选择= \\选择\\:?)> @ c.CountryName&LT ; /选项>
}
}
< /选择>
这是输出:
<选择自动对焦选择=\\&安培; QUOT;选定\\&放大器; QUOT;&安培; QUOT;:&放大器; QUOT;&安培; QUOT;)(美国= =值=AF>与阿富汗LT; /选项>
我是什么做错了,我该如何解决?我也试过,但得到一个异常:
@ Html.DropDownList(BusinessCountry,新的SelectList(国家,国家code,国家或地区名称,@ ViewBag.part.BusinessCountry),国家)
已经想出如何与code我做:
<选择一个id =BusinessCountryNAME =BusinessCountry级=验证[需要]parentTab =TAB4的风格=宽度:160像素;>
@foreach(C国的国家){
字符串SEL =(ViewBag.part.BusinessCountry == c.Country code选择= \\选择\\:);
<期权价值=@ c.Country code@sel> @ c.CountryName< /选项>
}
< /选择>
在视图中混合了很多code是一种错误的方式来做到这一点。还使用 ViewBag
/ 的ViewData code>来这样的操作方法和视图之间传输数据,使您的code丑陋。你应该考虑一个视图模型将数据从操作方法转移到查看。
假设你的看法是建立一个公司信息,有这样的视图模型
公共类CompanyViewModel
{
公共字符串名称{集;获取;}
公共IEnumerable的< SelectListItem>国家{集;获取;}
公众诠释{SELECTEDCOUNTRY设置;获取;} CompanyViewModel()
{
国家=新的List< SelectListItem>();
}
}
现在在你的 GET
Action方法,你会填充数据的视图模型对象的国家
收集和发送到视图。
公众的ActionResult的Create()
{
CompanyViewModel VM =新CompanyViewModel();
//下面的线是硬的演示codeD。你可能取代
//这与您的数据访问层/现有数组装载数据
vm.Countries =新[]
{
新SelectListItem {值=1,文本=美国},
新SelectListItem {值=2,文本=加拿大},
新SelectListItem {值=3,文本=澳洲}
};
返回查看(VM);
}
现在在你的强类型来看,
@model CompanyViewModel
@using(Html.Beginform())
{
@ Html.DropDownListFor(X => x.SelectedCountry,
新的SelectList(Model.Countries,值,文本),选择..)
<输入类型=提交/>}
现在在你的 HTTPPost
方法,你会被访问的的 SelectecCountry
属性值获得选定的国家ID型号发布
[HttpPost]
公众的ActionResult创建(CompanyViewModel模型)
{
如果(ModelState.IsValid)
{
//检查model.SelectedCountry属性值这里
//保存并重定向
}
//在这里刷新国家
返回查看(模型);
}
Trying to load a dropdownlist from an array of Countries:
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>
This is the output:
<option af?"selected="\"selected\"":"")" (us="=" value="AF">Afghanistan</option>
What am I doing wrong and how can I fix it? I've also tried this but get an exception:
@Html.DropDownList("BusinessCountry", new SelectList(Countries, "CountryCode", "CountryName", @ViewBag.part.BusinessCountry), Countries)
Already figured out how to do it with the code I have:
<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>
Mixing a lot of code in the view is a wrong way to do this. Also usage of ViewBag
/ViewData
to transfer data like this between action methods and views, makes your code ugly. You should consider a ViewModel to transfer the data from action method to view.
Assuming your view is to create a Company Details, Have a view model like this
public class CompanyViewModel
{
public string Name { set;get;}
public IEnumerable<SelectListItem> Countries { set;get;}
public int SelectedCountry { set;get;}
CompanyViewModel()
{
Countries=new List<SelectListItem>();
}
}
Now in your GET
Action method, you will fill the data to the Countries
collection of the viewModel object and send that to the 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);
}
Now in your strongly typed view,
@model CompanyViewModel
@using(Html.Beginform())
{
@Html.DropDownListFor(x => x.SelectedCountry,
new SelectList(Model.Countries,"Value","Text"), "Select..")
<input type="submit" />
}
Now in your HTTPPost
method, you will get the Selected country id by accessing the SelectecCountry
Properties value of the Model posted
[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);
}
这篇关于从阵列MVC3下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!