asp.net mvc 3 - Re-using code in controller class -


The following code is taken from the tutorial: which shows that ASP.X MVC 3 is used to manage the film's database can be done.

In the trainee, a list object is added to the controller class, in which the style of each movie is present in the database, then this list is seen near the drop-down, so that the database can be searched. Could.

Controller: (Code related to movie style in bold) Public Functional SearchInx (String Navigator, String Search String) {var GenreLst = New list & lt; String & gt; (); Var GenreQry = d to DB Choose d.Genre d.Genre by Movie Order; GenreLst.AddRange (GenreQry.Distinct ()); ViewBag.movieGenre = New Selection List (GenreList); DB in Var Films = M. Selection of Moms; If (String.IsNullOrEmpty (searchString)) {movies = movies.Where (s = & gt; s.Title.Contains (searchString)); } If (string.IsNullOrEmpty (Movie Ganner)) see return (movies); Else {Return View (movie where (x => x.Genre == movie generator)); }}

I want to proceed with it so that movies can be searched by price along with style. I know that I can reuse a lot of code to do this. I think that I need to create a new class that the controller class can either exceed the style or value. is this correct? If so, I would appreciate an example. Thank you.

Update / Explanation:

I would like to avoid replacing the code for the following style and value:

  Public Performance SearchInx (String Movigator, String SearchString, Float Movie Price) {var GenreLst = New list & lt; String & gt; (); Var GenreQry = d to DB Select d.Genre d.Genre by Movie Order; GenreLst.AddRange (GenreQry.Distinct ()); ViewBag.movieGenre = New Selection List (GenreList); Var pricelist = new list & lt; String & gt; (); Var from primitive = db Movie order by d. Jenner D. Choose Generator; PriceLst.AddRange (GenreQry.Distinct ()); ViewBag.moviePrice = new selection list (price list); DB in Var Films = M. Selection of Moms; If (String.IsNullOrEmpty (searchString)) {movies = movies.Where (s = & gt; s.Title.Contains (searchString)); } If (string.IsNullOrEmpty (Movie Ganner)) see return (movies); Else {Return View (movie where (x => x.Genre == movie generator)); } See if (string.IsNullOrEmpty (movie piece)) (movies); Else {Return View (Movie where (x => x.Genre == movie price)); }}    

You only have to insert a text box to get the price price. Then get this value in action and modify the query to get the desired results.

Like this:

  @ html.ActionLink ("Create New", "Create") @ (Html.BeginForm ()) {& lt; P & gt; Genre: @ Html.DropDownList ("Movie Generator", "All") Title: @ Html.TextBox ("SearchString") Price: @ Html.TextBox ("Price") & Lt; Input type = "submit" value = "filter" /> & Lt; / P & gt; }   

And in the verb method, you are using the code below to populate the drop downdown with style values. You should not do this for the price value.

  var GenreLst = new list & lt; String & gt; (); Var GenreQry = d to DB Choose d.Genre d.Genre by Movie Order; GenreLst.AddRange (GenreQry.Distinct ()); ViewBag.movieGenre = New Selection List (GenreList);   

And in your operation method, you have to use the value value to filter the data

  public action searchindex (string moviegenerator, string search string , Float price) {Var GenreLst = New list & lt; String & gt; (); Var GenreQry = d to DB Select d.Genre d.Genre by Movie Order; GenreLst.AddRange (GenreQry.Distinct ()); ViewBag.movieGenre = New Selection List (GenreList); DB in Var Films = M. Selection of Moms; If (String.IsNullOrEmpty (searchString)) {movies = movies.Where (s = & gt; s.Title.Contains (searchString)); } If (string.IsNullOrEmpty (Movie Ganner)) see return (movies); Other {Return View (movies.where ((x = x.Genre == movie generator) & amp; amp; (x => x.Price == value)); }}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -