Reputation: 177
I'm using MVC Razor's @Html.DropDownList to generate dropdown list.
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name
});
ViewBag.cSfRR = cSfRR;
This TABLE has one more field which determines activity of each item in the table.
If I use following code I get that activity field included after c.Name
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name + " - " + c.Activity
});
ViewBag.cSfRR = cSfRR;
My problem is that that field has only one character which determines it's activity. A - Active, N - Not active. I would like to show custom text "Not Active" for those items in SelectListItem that have value "N". Something like...
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name + " - " + IF C.ACTIVITY IS "N", THEN PRINT "NOT ACTIVE"
});
ViewBag.cSfRR = cSfRR;
I hope someone can help me achieve something like this
DROPDOWN LIST ITEM 1
DROPDOWN LIST ITEM 2
DROPDOWN LIST ITEM 3
DROPDOWN LIST ITEM 4
DROPDOWN LIST ITEM 5 - NOT ACTIVE
DROPDOWN LIST ITEM 6 - NOT ACTIVE
DROPDOWN LIST ITEM 7 - NOT ACTIVE
Upvotes: 1
Views: 1237
Reputation: 746
You could try something like this:
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name + (c.Activity == "N") ? " - Not Active " : "";
});
ViewBag.cSfRR = cSfRR;
Upvotes: 0
Reputation: 62311
You can use conditional operator - ?: Operator
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name + (c.Activity == "N" ? " - NOT ACTIVE" : "")
});
ViewBag.cSfRR = cSfRR;
Upvotes: 2
Reputation: 8485
I don't promote the use of ViewBag
, but this should work:
IEnumerable<SelectListItem> cSfRR = db.TABLE.Where(m => m.cRoleName == dbrole)
.Select(c => new SelectListItem
{
Value = c.ID,
Text = c.Name + (c.ACTIVITY == "N" ? " - NOT ACTIVE" : string.empty)
});
ViewBag.cSfRR = cSfRR;
Upvotes: 2