Ancient
Ancient

Reputation: 3057

MVC - How to add css property on dropdown based on condition

I started using MVC after a long time and now I am unable to do some things really easy.

Model class:

public class TaskDetails
{
    public string ProjectID { get; set; }
    public string ProjectName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EstimatedDate { get; set; }
    public string TaskDescription { get; set; }
}

Controller:

List<SelectListItem> query = DE.tblEmployees
                               .Select(c => new SelectListItem { Text = c.Name, Value = c.Name })
                               .ToList();
ViewBag.Categories = query;
return View();

View:

<div class="dropdown">
@Html.DropDownList("CategoryID", (List<SelectListItem>)ViewBag.Categories, "--User Name--")
</div>

This is the source and its working perfectly, but what I want to do is to hide all specific options in dropdown where Value != NewTask

Upvotes: 0

Views: 372

Answers (2)

Vitaly Zhynov
Vitaly Zhynov

Reputation: 51

Not sure is solution for you, but just in case.

 List<SelectListItem> query = DE.tblEmployees
    .Select(c => new SelectListItem { Text = c.ProjectName, Value = c.ProjectName })
    .Where(x => x.Value.Equals("NewTask"))
    .ToList();

Or in view directly:

@Html.DropDownList("CategoryID", ((List<SelectListItem>)ViewBag.Categories).Where(x => x.Value.Equals("NewTask")), "--User Name--")

Upvotes: 1

Mike
Mike

Reputation: 83

If I'm not mistaken, you want to show in your dropdown the ProjectIDs which are greater than 10, right?

You can do that at your controller,

DE.tblEmployees.Select(c => new SelectItem
                      {Text = c.Name, Value = c.Name }).Where(x => x.ProjectID > 10).ToList();

Hope this will help.

Upvotes: 0

Related Questions