开发者

problem with Html.DropDownFor and selected item

开发者 https://www.devze.com 2023-02-03 12:23 出处:网络
I can\'t figure Html.DropDownFor out to work properly when SelectListItem Text is different from Value. This issue is when marking an specific item as selected=\"true\" and everything is failed! (Noth

I can't figure Html.DropDownFor out to work properly when SelectListItem Text is different from Value. This issue is when marking an specific item as selected="true" and everything is failed! (Nothing is marked as selected="true")

public IEnumerable<SelectListItem> HaveFail {
        get {
            return
                IoC.Container.Resolve<Something>().GetAll().Select(
                    x =>
                    new SelectListItem {
                                           Text = x.Key,
 开发者_开发百科                                          Value = x.Value.ToString(),
                                           Selected = (string.Compare(x.Key, myValue) == 0)
                                       });
        }
    }

public IEnumerable<SelectListItem> WorksFine {
        get {
            return
                IoC.Container.Resolve<Something>().GetAll().Select(
                    x =>
                    new SelectListItem {
                                           Text = x.Key,
                                           Value = x.Key,
                                           Selected = (string.Compare(x.Key, myValue) == 0)
                                       });
        }
    }


You've got it backwards.

new SelectListItem 
{
    Text = x.Value.ToString(),
    Value = x.Key,
    Selected = (string.Compare(x.Key, myValue) == 0)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号