简体   繁体   English

奇怪的DropDownListFor / SelectListItem问题未设置默认值

[英]Weird DropDownListFor / SelectListItem issue not setting default value

I'm pulling my hair out here. 我在这里拔头发。 I'm using the DropDownListFor HTML helper in MVC3 with Enums like so: 我在MVC3和Enums中使用DropDownListFor HTML助手,如下所示:

@Html.DropDownListFor(model => model.Title, Enum.GetValues(typeof(ICS_Signup_Form.Models.Titles)).Cast<ICS_Signup_Form.Models.Titles>().Select(x => new SelectListItem { Text = x.ToString().ToFriendlyString(), Value = x.ToString() }), new { @class = "styled" })

My enum looks like so: public enum Titles { Mr, Dr, Miss, Mrs, Ms, Other }; 我的枚举看起来像这样: public enum Titles { Mr, Dr, Miss, Mrs, Ms, Other };

If Model.Title equals "Other" then the drop down list doesn't select this value, there's no selected="selected" in the HTML. 如果Model.Title等于“其他”,则下拉列表不会选择此值,因此HTML中没有selected="selected" I've tried adding the property Selected to SelectListItem : Selected = (x.ToString() == Model.Title) and the expression works fine when I'm stepping through my code, as expected but the selected value is always "Mr" (the first in the list). 我尝试将属性Selected添加到SelectListItemSelected = (x.ToString() == Model.Title) ,当我逐步执行代码时,表达式可以正常工作,但是选择的值始终是“ Mr” (列表中的第一个)。

What's even weirder this works perfectly fine (as do the other 7 drop down boxes I have in my project): 更奇怪的是,它的工作原理非常好(我项目中的其他7个下拉框也是如此):

@Html.DropDownListFor(model => model.BusinessStatus, Enum.GetValues(typeof(ICS_Signup_Form.Models.BusinessTypes)).Cast<ICS_Signup_Form.Models.BusinessTypes>().Select(x => new SelectListItem { Text = x.ToString().ToFriendlyString(), Value = x.ToString() }), new { @class = "styled" })

With the enum: public enum BusinessTypes { Charity, Government, Limited, LLP, Partnership, PLC, SoleTrader }; 带有枚举: public enum BusinessTypes { Charity, Government, Limited, LLP, Partnership, PLC, SoleTrader };

The difference? 区别? None.. Any ideas? 没有。有什么想法吗?

tldr; tldr; change the name of your DropDownList to something else 将您的DropDownList的名称更改为其他名称

I had the same problem (albeit in ASPX engine, which I believe doesn't make any difference in this case). 我遇到了同样的问题(尽管在ASPX引擎中,我认为在这种情况下没有任何区别)。 Here's how to repro it: 复制方法如下:

the codebehind (note "Selected = true" in the second item): 后面的代码(在第二项中注意“ Selected = true”):

List<SelectListItem> teams = new List<SelectListItem>();
teams.Add(new SelectListItem { Text = "Slackers", Selected = false, Value = "0" });
teams.Add(new SelectListItem { Text = "Workers", Selected = true, Value = "1" });
ViewData["teams"] = teams;

the ASPX code: ASPX代码:

<%: Html.DropDownList("team", (IEnumerable<SelectListItem>)ViewData["teams"]) %>

It reproes: you can see that the first item gets selected automatically and neither of the items have the "selected" option in HTML. 它的含义是:您可以看到第一个项目被自动选择,并且两个项目都不具有HTML中的“ selected”选项。 Here's the generated HTML which reproes it: 这是生成的HTML代码,可以对其进行谴责:

<select id="team" name="team">
<option value="0">Slackers</option>
<option value="1">Workers</option>
</select>

Solution : Turns out some of the element names cause the ASPX rendering engine to behave differently. 解决方案 :结果是某些元素名称导致ASPX呈现引擎的行为有所不同。 In my case, I just changed the DropDownList name from "team" to "defaultteam" -- and it started working: 就我而言,我只是将DropDownList名称从“ team”更改为“ defaultteam”-它开始工作:

Here's WORKING ASPX code: 这是工作的 ASPX代码:

<%: Html.DropDownList("defaultteam", (IEnumerable<SelectListItem>)ViewData["teams"]) %>

and here is the HTML generated by it: 这是它生成的HTML:

<select id="defaultteam" name="defaultteam">
<option value="0">Slackers</option>
<option selected="selected" value="1">Workers</option>
</select>

It's strange. 真奇怪。 I am unable to repro. 我无法复制。 Here's my working code. 这是我的工作代码。

Model: 模型:

public enum Titles { Mr, Dr, Miss, Mrs, Ms, Other };

public class MyViewModel
{
    public Titles Title { get; set; }
}

Controller: 控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Title = Titles.Other
        };
        return View(model);
    }
}

View: 视图:

@model MyViewModel

@Html.DropDownListFor(
    model => model.Title, 
    Enum.GetValues(typeof(Titles))
        .Cast<Titles>()
        .Select(x => new SelectListItem 
        { 
            Text = x.ToString(), 
            Value = x.ToString() 
        }), 
    new { @class = "styled" }
)

As expected, Other is preselected: 与预期的一样,“ Other已预先选择:

<select class="styled" id="Title" name="Title">
    <option value="Mr">Mr</option>
    <option value="Dr">Dr</option>
    <option value="Miss">Miss</option>
    <option value="Mrs">Mrs</option>
    <option value="Ms">Ms</option>
    <option selected="selected" value="Other">Other</option>
</select>

You might also find the following extension method useful. 您可能还会发现以下扩展方法很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM