Html.DropDownList 中可能存在错误,所选值未反映?

发布于 2024-10-08 14:47:49 字数 868 浏览 0 评论 0原文

我知道这个问题之前已经被问过几次了,但是现有的解决方案看起来像是黑客而不是正确的模式。

我有一个简单的下拉列表。我正在尝试用数据填充它并预先选择一个项目。我成功地“用数据部分填充它”,但未能预先选择我的首选项目。

我正在使用 ASP.NET MVC 3 RC2。

重要的代码位:

{
    // Bunch of code
    CategoryOptions = new SelectList(categories, "Id", "Slug", article.Category.Id);
}

现在... CategoryOptions 被传递到 Html 帮助器,如下所示:

@Html.DropDownListFor(a => a.Category, Model.CategoryOptions)

我已经查看了传递到帮助器的值,并确认其中一项的选定值设置为 true:

alt text

但是,它没有反映在助手生成的 Html 中。所有 ... 标签都没有 selected="selected" 属性。

我使用 Reflector 稍微检查了一下代码,这行 (SelectExtensions.SelectInternal) 看起来很危险:

item.Selected = (item.Value != null) ? set.Contains(item.Value) : set.Contains(item.Text);

我在这里做错了什么吗?或者是框架(ASP.NET MVC 3 RC2)有问题。

I know this has been asked a few times before, but the existing solutions look like hacks rather than a proper pattern.

I have a simple drop down list. I am trying to populate it with data and have a item pre selected. I am succeeding at the 'populate it with data part' but failing to get my preferred item pre-selected.

I am using ASP.NET MVC 3 RC2.

The important code bits:

{
    // Bunch of code
    CategoryOptions = new SelectList(categories, "Id", "Slug", article.Category.Id);
}

Now... the CategoryOptions is being passed to the Html helper like so:

@Html.DropDownListFor(a => a.Category, Model.CategoryOptions)

I have looked at the values being passed into the helper and have confirmed that one of the items has the selected value set to true:

alt text

However, it is not reflecting in the Html being generated by the helper. None of the ... tags have the selected="selected" attribute.

I used Reflector to examine the code a bit, and this line (SelectExtensions.SelectInternal) looks dicey:

item.Selected = (item.Value != null) ? set.Contains(item.Value) : set.Contains(item.Text);

Am I doing something wrong here? Or is the framework (ASP.NET MVC 3 RC2) at fault here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞 2024-10-15 14:47:49

好吧...我刚刚修好了。

我更改了视图模型以使用 CategoryId 而不是 Category slug。所以现在,我的观点如下:

@Html.DropDownListFor(a => a.CategoryId, Model.CategoryOptions, new { @class = "ask-category-field" })

我认为选择的模型值(在本例中,a => a.CategoryId)必须具有与所选“值”相同的类型。 (当我有更多时间检查框架代码时,我将完善这个解释。)但确实是聪明的编码:)向 ASP.NET MVC 开发人员致敬。

现在,它按预期完美运行,并且还保留了整个 PRG 生命周期中对所选项目的更改。

希望这对某人有帮助。

Ok... I just fixed it.

I changed around my view model to use a CategoryId instead of a Category slug. So now, my view looks like:

@Html.DropDownListFor(a => a.CategoryId, Model.CategoryOptions, new { @class = "ask-category-field" })

I think the model value being select (in this case, a => a.CategoryId) has to have the same type as the selected 'value.' (I will refine this explanation when I get more time to go over the framework code.) But clever coding indeed :) Hats of to ASP.NET MVC devs.

Now, it works perfectly as expect and also retains changes to the selected item across a PRG lifecycle.

Hope this helps someone.

林空鹿饮溪 2024-10-15 14:47:49

下拉项上的 selected 属性实际上并未设置要在 HTML 中选择的下拉项。

我自己以前也经历过这个问题。

我的解决方案是 ViewDataDictionary 上的扩展方法,它允许您根据所选项目和列表设置给定选择列表的值。选择列表的字符串 ID。

public static void SetSelectedValueFor(this ViewDataDictionary viewDataDictionary, string key, IEnumerable<SelectListItem> items)
        {
            //  HACK: Store the selected item value in ViewData so that the DropDownListFor knows which one should be selected.
            //  It's a bit of a hack but it seems to be the only way I can do it since DropdownListFor ignores the Selected property on SelectListItem.
            var x = items.Where(c => c.Selected).FirstOrDefault();
            if (x != null)
            {
                viewDataDictionary[key] = x.Value;
            }
        }

The selected property on a dropdown item doesn't actually set the dropdown item to be selected in the HTML.

I've experienced this problem myself before.

My solution to this was an extension method on the ViewDataDictionary that allows you to set the value of a given select list based on the selected item & a string Id of the select list.

public static void SetSelectedValueFor(this ViewDataDictionary viewDataDictionary, string key, IEnumerable<SelectListItem> items)
        {
            //  HACK: Store the selected item value in ViewData so that the DropDownListFor knows which one should be selected.
            //  It's a bit of a hack but it seems to be the only way I can do it since DropdownListFor ignores the Selected property on SelectListItem.
            var x = items.Where(c => c.Selected).FirstOrDefault();
            if (x != null)
            {
                viewDataDictionary[key] = x.Value;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文