0
var userColor = db.userColor;
foreach(var x in userColor){
    var correspondingDropDownValue = from m in db.user
                                     where m.UserID == x.UserID
                                     select m;

 ViewData["dropDown_Color"] = correspondingDropDownValue.Select(j => new SelectListItem { Text = j.ListOfValue, Value = j.ListOfValue }).ToList();
}

在这个 db.userColor 表中,还有一个称为 db.userColor.default 的列,它带有一个用作默认值的值。如何修改上面的代码来设置选定的值?

4

2 回答 2

1

使用 的Selected属性SelectListItem来确定默认情况下应该选择哪个:

ViewData["dropDown_Color"] = correspondingDropDownValue.Select(j => new SelectListItem { Text = j.ListOfValue, Value = j.ListOfValue, Selected = j.ListOfValue == j.userColor.default }).ToList();

无法看到您的架构,我猜在哪里userColor.default,但您可以根据需要更新逻辑。

于 2013-08-27T11:27:19.577 回答
0
Use the below code to set property 

 List<LimitType> objLimitTypeList = new List<LimitType>();

        LimitType objLimitType = new LimitType();
        objLimitType.WithinLimitCode = 0;
        objLimitType.WithinLimitName = "Within Limit";
        objLimitTypeList.Add(objLimitType);

        LimitType objLimitTypeN = new LimitType();
        objLimitTypeN.WithinLimitCode = 1;
        objLimitTypeN.WithinLimitName = "Over Limit";
        objLimitTypeList.Add(objLimitTypeN);

        if (BtnAct.ToLower() == "add")
        {
            ViewBag.WithinLimit = new SelectList(objLimitTypeList, "WithinLimitCode", "WithinLimitName");
        }
        else
        {
            if (IsWithinLimit.ToLower() == "y")
                ViewBag.WithinLimit = new SelectList(objLimitTypeList, "WithinLimitCode", "WithinLimitName", 0);
            else
                ViewBag.WithinLimit = new SelectList(objLimitTypeList, "WithinLimitCode", "WithinLimitName", 1);

        }
于 2014-08-27T06:45:00.313 回答