0

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

2
  • 1
    What is NewTask ? Commented Nov 20, 2017 at 14:56
  • Text = c.Name, Value = c.Name are you sure ?? Commented Nov 20, 2017 at 16:53

2 Answers 2

1

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--")
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Have you answered the right question? Sorry, after looking more, you have - just don't know where you are getting the specific project ids > 10 bit from (unless question was edited and edit hasn't shown up)
Sorry, I added that first by mistake please see update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.