DropDownListFor ...选定值 [英] DropDownListFor ... selected value

查看:141
本文介绍了DropDownListFor ...选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型,我的国家的名单: Model.ListCountry 。乡村类有一些字段:
ID,code,ValueFR,ValueUS

In my model, I have a list of country : Model.ListCountry. The Country class has some fields : Id, Code, ValueFR, ValueUS

在我的模型中,我有一个客户,这客户有一个国家: Model.Customer.Country

In my model, I have a Customer and this customer has a country : Model.Customer.Country

我试过这样:

@Html.DropDownListFor(x => x.Record.Customer.Country, new SelectList(Model.ListCountry, "Code", "FR"), new { id = "lbCountry" })

没有想法?

谢谢,

UPDATE1:
在数据库中,我保存ID,但在下拉列表中的选项值我用code,并显示fiels ValueFR或ValueUS取决于语言的用户

Update1: In the database, I save the Id, but in the dropdown as "option value" I use the code, and as display fiels ValueFR or ValueUS depending of the language user

推荐答案

在为preSELECT在下拉列表中值设置相应的属性值,这在你的控制器动作:

In order to preselect a value in a dropdown list set the corresponding property to this value in your controller action:

model.Record.Customer.Country = "FR";

至于下拉列表一代而言要传递到的SelectList构造<两个字符串参数/ A>重新present分别对应的值和文本模型的属性名称。所以我想它应该是更喜欢这样的:

As far as the dropdown list generation is concerned the two string arguments you are passing to the SelectList constructor represent the property names of the model corresponding respectively to the Value and Text. So I guess it should be more like this:

@Html.DropDownListFor(
    x => x.Record.Customer.Country, 
    new SelectList(Model.ListCountry, "Code", "ValueFR"), 
    new { id = "lbCountry" }
)

在这个例子中,我们使用 code 属性作为在下拉列表和 ValueFR 属性的值为文本。因此,在这种情况下,你必须确保你正在设置 model.Record.Customer.Country 属性一些 code 存在于列表和下拉菜单会自动preSELECT这个项目。

In this example we are using the Code property as Values in the dropdown list and ValueFR property as Text. So in this case you must ensure that you are setting the model.Record.Customer.Country property to some Code that exists in the list and the dropdown will automatically preselect this item.

另一种可能使用以下的SelectList构造它允许你指定一个选择的值作为4 参数:

Another possibility use the following SelectList constructor which allows you to specify a selected value as 4th parameter:

@Html.DropDownListFor(
    x => x.Record.Customer.Country, 
    new SelectList(Model.ListCountry, "Code", "ValueFR", "FR"), 
    new { id = "lbCountry" }
)

这篇关于DropDownListFor ...选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆