Html.DropdownList不同的行为 [英] Html.DropdownList different Behaviour

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

问题描述

遇到这种奇怪的行为,需要找出到底是怎么回事错在这里。

Came across this strange behavior, need to find out what is going wrong here.

我们有下拉列表,列出了不同的语言,选择当前的语言。

We have Dropdown List, listing the different language and select the current language.

下面是code建设SelectListItem.Note的列表中选定的属性。

Here is the code for building the List of SelectListItem.Note the Selected property

  var languages = from lang in CultureManager.SupportedCultures
                        select new SelectListItem()
        {
            Value= lang.Key,
            Text = lang.Key,
           Selected= CultureInfo.CurrentCulture.TwoLetterISOLanguageName.Equals(lang.Key,StringComparison.InvariantCultureIgnoreCase)
        };

        List<SelectListItem> LanguageList = languages.ToList<SelectListItem>();   
        ViewBag.Languages = LanguageList;

在剃刀查看

  @Html.DropDownList("Languages","Select Language")

结果:
它按预期工作,它会创建的DropDownList和选择的文化语言,其中选择PROPERT是真实的。

Outcome: It work as expected, it will create the dropdownlist and select the culture language where selected propert is true.

如果我们改变的Razor视图如下,它会创建的DropDownList但如果所选属性为true不会选择SelectListItem。

if we change the Razor View as below , it will create the dropdownlist but will not select the SelectListItem where Selected Property is true.

  @Html.DropDownList("Languages", (IEnumerable<SelectListItem>)ViewBag.Languages, "Select Language", new { @class="form-control"})

在这里是什么&放大器发生任何人可以帮助;在解决这个问题。

Can anybody help in what is happening here & in resolving this issue.

推荐答案

它不奇怪可言,而且是预期的行为。

Its not strange at all, and is the expected behavior.

在第一个例子中,你也没有约束力的下拉到您模型中的任何财产。在内部,助手只是建立&LT;选择名称=语言&GT; 根据您的 SelectListItem 收集的选项和选择的值设置到选择= true的第一个项目

In the first example you are not binding your drop down to any property in you model. Internally the helper just builds <select name="Languages"> with the options based on your collection of SelectListItem and sets the value of select to the first item where Selected=true.

在第二个例子中,您使用您的努力降绑定到你的模型命名为属性语言使用 ViewBag超载.Languages​​ 属性创建的选项。因为你绑定到一个属性它忽略 SelectListItem.Selected 的价值。其预计,如果您设置的模型属性语言=德国然后选择值=德国将被选中(如果它基于 SelectListItem.Selected 的选择,那么你将永远无法绑定到你的视图模型属性)。在你的情况,因为你的模型没有一个命名属性语言及其,所以它的第一个选项匹配 - 的一个接使用具有辅助的第三个参数创建&LT;选项值&GT;选择语言&LT; /选项&GT; (实际上它确实找到 A值ViewData的名为语言(你的 ViewBag 属性),但它不是一个值类型,因此不能被匹配选项值)。

In the second example, your using the overload where your trying to bind the drop down to a property in your model named Languages using the ViewBag.Languages property to create the options. Because your binding to a property it ignores the value of SelectListItem.Selected. Its expected that if you have set the model property Languages="German" then the option with value="German" would be selected (if it based the selection on SelectListItem.Selected then you would never be able to bind to your view to your model properties). In your case because your model does not have a property named Languages its null and so it matches the first option - the one created by using the 3rd parameter of the helper which has <option value>Select Language</option> (actually it does find a value in ViewData named Languages (your ViewBag property) but its not a value type so cant be matched to an option value).

使用的DropDownList 这样是不好的做法,你应该总是使用强类型的辅助。你的模型应该包含一个属性绑定到,说

Using DropDownList this way is not good practice and you should always used strongly typed helpers. You model should contain a property to bind to, say

public string SelectedLanguage { get; set; }

,然后在您查看

@Html.DropDownListFor(m => m.SelectedLanguage, (IEnumerable<SelectListItem>)ViewBag.Languages, "Select Language", new { @class="form-control"})

现在你已经强类型的双向绑定。如果值 SelectedLanguage =德国,然后在页面加载时,如果用户选择其他选项,则该值将被绑定到该选项将被选中你模型上回发。

Now you have strongly typed 2 way binding. If the value of SelectedLanguage="German", then that option will be selected when the page loads, and if the user selects another option, then that value will be bound to your model on post back.

这篇关于Html.DropdownList不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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