I have the following action method that initiated SelectList as follow:-
public PartialViewResult Search(string q, int q2,string q3,string sortOrder)
{
ViewBag.q2 = new SelectList(elearningrepository.FindAllLevels().ToList(), "DifficultyID", "Description", 2);
ViewBag.q3 = new SelectList(elearningrepository.FindAllusers().ToList(), "UserID", "UserID",2);
ViewBag.today = DateTime.Today;
ViewBag.nextmonth = DateTime.Now.AddMonths(1);
var v = elearningrepository.searchquestions3(q, q2, q3);
return PartialView("_searchquestion", v);
}
And the following repository method:-
public IQueryable<Question> searchquestions3(string q, int? q2 , string)
{
return from u in entities1.Questions
where (u.Description.Contains(q) && (u.DifficultyID == q2 || q2 == "Any" ) && ( u.CreatedBy == q3 || q3 == "Any"))
select u;}
then on the view i render the drop down list as follow:-
<th>
@Html.DropDownList("q2")
</th>
</tr>
<tr>
<th>
Seach By Create By:-
</th>
<th>
@Html.DropDownList("q3")
</th>
But how i can add a new value that represents “Any” word to the q2 & q3 drop down lists, so that the repository method will work as intended ?
BR
First of all I would advice you to use ViewModels when you work with more complex objects. You can achieve it with the ViewBag of course, but it’s messy.
Now. In the action you can do:
You can do adequatly with the others, but seriously think about strongly typed ViewModel.