dataflowjoe
asked on
Asp.net MVC shared method
Hi experts,
I'm dabbling with ASP.net MVC and slowly working through this excellent tutorial . I've got to the point where I've created a lookup list of movie genres which is used for searching the list of movies. I now realise that it would be useful to have the same list when creating or editing a movie. I realise that it is bad practice to recreate the code each time for the routing methods. So how would I organise the code so it is placed in one location and then referenced from there wherever I need it.
Any help would be appreciated.
Joe
I'm dabbling with ASP.net MVC and slowly working through this excellent tutorial . I've got to the point where I've created a lookup list of movie genres which is used for searching the list of movies. I now realise that it would be useful to have the same list when creating or editing a movie. I realise that it is bad practice to recreate the code each time for the routing methods. So how would I organise the code so it is placed in one location and then referenced from there wherever I need it.
public ActionResult SearchIndex(string movieGenre, string searchString)
{
//+++++++++START OF GENRE LIST CREATION+++++++++++++++
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
//+++++++++END OF GENRE LIST CREATION+++++++++++++++
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(movieGenre))
return View(movies);
else
{
return View(movies.Where(x => x.Genre == movieGenre));
}
}
public ActionResult Create()
{
//++++++I'd like to create the genre list here before being passed
//to the create movie view without repeating the section of code
//from the searchIndex method above++++++++
return View();
}
I'm guessing that I create a public method within the same class and call it from both of the other methods.Any help would be appreciated.
Joe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER