问题DropDownListFor的SelectedItem [英] Problem with DropDownListFor SelectedItem

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

问题描述

这已经彻底困惑了我。

这是我的观点:

@Html.DropDownListFor(model => model.ScoreDescription, 
                               Model.RatingOptions, 
                               "--", 
                               new { @id = clientId })

和模型:

public decimal? Score { get; set; }
public SelectList RatingOptions
{ 
    get
    {
        var options = new List<SelectListItem>();

        for (var i = 1; i <= 5; i++)
        {
            options.Add(new SelectListItem
            {
                Selected = Score.HasValue && Score.Value == Convert.ToDecimal(i),
                Text = ((decimal)i).ToRatingDescription(ScoreFactorType),
                Value = i.ToString()
            });
        }

        var selectList = new SelectList(options, "Value", "Text");
            // At this point, "options" has an item with "Selected" to true.
            // Also, the underlying "MultiSelectList" also has it.
            // Yet selectList.SelectedValue is null. WTF?
        return selectList;
    }
}

由于意见建议,我不能得到所选择的值的情况发生。

As the comments suggest, i can't get the selected value to happen.

难道是与我使用一个可空小数的事实呢?这个循环之后,选项是正确的,因为它正好有1个项目与选择为true,这样看来我做了正确的事情。

Is it something to do with the fact i'm using a nullable decimal ? After that loop, options is correct in that it has exactly 1 item with select to true, so it seems i'm doing the right thing.

现在,如果我使用的不同的的SelectList 过载:

Now, if i use a different SelectList overload:

var selectedValue = Score.HasValue ? Score.Value.ToString("0") : string.Empty;
var selectList = new SelectList(options, "Value", "Text", selectedValue);

它的工作原理。为什么?起初我以为这可能是一个LINQ的把戏(如延迟执行),但我试图迫使 .ToList()并没有什么区别。

这就像设置属性作为创建 SelectListItem 没有效果,你必须将它设置在使用的SelectList 构造函数参数结束。

It's like setting the Selected property as you create the SelectListItem has no effect, and you have you set it at the end using the SelectList ctor parameter.

任何人都可以阐明这一些轻?

Can anyone shed some light on this?

推荐答案

如果你看一下选择列表类的实现它从来没有真正使用要传递一个事实, SelectListItem 。它的工作原理与的IEnumerable 。因此, SelectListItem 未使用的选定属性。我个人preFER设置选择的设置所绑定的DDL到相应的属性值下拉值。

If you look at the implementation of the SelectList class it never actually uses the fact that you are passing a SelectListItem. It works with an IEnumerable. So the Selected property of a SelectListItem is not used. Personally I prefer setting the selected value of a dropdown by setting the value of the corresponding property that you are binding the ddl to.

例如:

public int? Score { get; set; }
public SelectList RatingOptions
{ 
    get
    {
        var options = Enumerable.Range(1, 5).Select(i => new SelectListItem
        {
            Text = ((decimal)i).ToRatingDescription(ScoreFactorType),
            Value = ((decimal)i).ToString()
        });
        return new SelectList(options, "Value", "Text");
    }
}

然后在控制器动作简直就是分数属性设置必要的值,并在视图中使用这个分数属性绑定到:

and then in the controller action simply set the Score property to the necessary value and in the view use this Score property to bind to:

@Html.DropDownListFor(
    model => model.Score, 
    Model.RatingOptions, 
    "--", 
    new { @id = clientId }
)

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

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