ASP.NET MVC:DropDownListFor不選擇任何選項

[英]ASP.NET MVC: DropDownListFor doesn't select any option


I have this to populate a drop down list in an ASP.NET MVC view.

我有這個填充ASP.NET MVC視圖中的下拉列表。

<%= Html.DropDownListFor(model => model.Bikes,
      Model.Bikes.Select(
      x => new SelectListItem {
               Text = x.Name,
               Value = Url.Action("Details", "Bike", new { bikeId = x.ID }),
               Selected = x.ID == Model.ID,
           })) %>

Debugging this I can see that the Selected property is set to true when it should be. But when the view is rendered, none of the options in the list is selected. I realize that this could be done with another overload of DropDownListFor but I really want to get this version working.

調試這個我可以看到Selected屬性應該設置為true。但是,在呈現視圖時,列表中的所有選項都未被選中。我意識到這可以通過DropDownListFor的另一個重載完成,但我真的想讓這個版本工作。

Any ideas?

2 个解决方案

#1


5  

The reason your selected value doesn't work is because you are using Urls as option values but specifying Model.ID in the Selected clause.

您選擇的值不起作用的原因是您使用Urls作為選項值,但在Selected子句中指定Model.ID。

Try like this:

試試這樣:

<%= Html.DropDownListFor(
    model => model.Bikes,
    new SelectList(Model.Bikes, "Id", "Name", Model.ID)
)%>

"Id" indicates the property that will be used as option value while "Name" indicates the property that will be used as option label.

“Id”表示將用作選項值的屬性,而“Name”表示將用作選項標簽的屬性。

If you want to preserve the Url.Action you could try:

如果您想保留Url.Action,可以嘗試:

<%= Html.DropDownListFor(
    model => model.Bikes,
    new SelectList(Model.Bikes.Select(x => new {
        Id = x.Id,
        Name = Url.Action("Details", "Bike", new { bikeId = x.ID })
    }), "Id", "Name", Model.ID)
)%>

You will notice that I've inverted the Name and Id as it seemed more logical to use the model Id as option value.

你會注意到我已經顛倒了Name和Id,因為使用模型Id作為選項值似乎更合乎邏輯。


UPDATE:

The reason this doesn't work is because you are binding to an IEnumerable (the first argument of the DropDownListFor helper). It should be a scalar property while you are using the same model.Bikes collection:

這不起作用的原因是因為您綁定到IEnumerable(DropDownListFor幫助器的第一個參數)。當你使用相同的模型時它應該是一個標量屬性。自行車集合:

<%= Html.DropDownListFor(
    model => model.SelectedBikeValue,
    Model.Bikes.Select(
        x => new SelectListItem {
                 Text = x.Name,
                 Value = Url.Action("Details", "Bike", new { bikeId = x.ID }),
                 Selected = x.ID == Model.ID,
        }
)) %>

My remark about not using Urls as option values stands true.

關於不使用Urls作為選項值的說法是正確的。

#2


0  

I've had so many troubles with this concept. It especially manifests itself when you cannot pass it "name" property from the model that contains that property in an object, because such object name is automatically prepended to the name. This is crazy. After wasting many hours trying to figure this one out I just gave up and wrote my own Drop-Down extension which I publish here. It is very simple and it works just fine.

我對這個概念有很多麻煩。當您無法從包含該對象的屬性的模型中傳遞“name”屬性時,它尤其會顯示自身,因為此類對象名稱會自動添加到名稱之前。這太瘋狂了。在浪費了很多時間試圖弄清楚這個之后我就放棄並編寫了我自己的Drop-Down擴展,我在這里發布了。它非常簡單,工作得很好。

    public static MvcHtmlString SimpleDropDown(this HtmlHelper helper, object attributes, IEnumerable<SelectListItem> items, bool disabled = false)
    {
        XElement e = new XElement("select",
            items.Select(a => {
                XElement option = new XElement("option", a.Text);
                option.SetAttributeValue("value", a.Value);
                if (a.Selected)
                    option.SetAttributeValue("selected", "selected");
                return option;
            })
            );

        if (attributes != null)
        {
            Dictionary<string, string> values = (from x in attributes.GetType().GetProperties() select x).ToDictionary(x => x.Name, x => (x.GetGetMethod().Invoke(attributes, null) == null ? "" : x.GetGetMethod().Invoke(attributes, null).ToString()));
            foreach(var v in values)
                e.SetAttributeValue(v.Key, v.Value);
        }

        if (disabled)
            e.SetAttributeValue("disabled", "");

        return new MvcHtmlString(e.ToString());
    }

Also, I put flag disabled as an extra parameter, because if you want to bind to it via the standard anonymous list of attributes, it can be quite a hassle.

此外,我將標志禁用作為額外的參數,因為如果你想通過標准的匿名屬性列表綁定它,它可能是一個非常麻煩。

Below is an example of how I use it at the moment. I translate a dictionary into a list of SelectListItem, but it can be just a simple list.

以下是我目前如何使用它的示例。我將字典翻譯成SelectListItem列表,但它可以只是一個簡單的列表。

@Html.SimpleDropDown(new { id="EOM", name = "EOM", @class = "topBox" }, Model.EOM.Select(x => new SelectListItem { Text = x.Value, Value = x.Key.ToString(), Selected = Model.EOM.Selected == x.Key }), !Model.EOM.Available)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/07/28/72fa3f19e6f5f1f133ab88fd5026a918.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com