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();
}

In this db.userColor table has another column known as db.userColor.default which carry a value that to be used as the default value. How can I modify the code above to set the selected value?

2 Answers 2

1

Use the Selected property of SelectListItem to determine which should be selected by default:

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

Not being able to see your schema, I'm guessing where userColor.default is, but you can update the logic as necessary.

Sign up to request clarification or add additional context in comments.

Comments

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);

        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.