MVC Passing Parameters from one view to another through Query Strings

Pass Parameter to another view of another controller:

In view:

@Html.ActionLink(“Movies”, “Index”, “Movie”, new { CategoryId = item.Id}, null)

Where:

  • 1st par “Movies” refers to the link text visible to user
  • 2nd par “Index” is the page you want to link to in the other view
  • 3th par “Movie” is the other View folder you want to link to
  • 4th par is the actual parameter you want to pass, in this case we will be passing category Id to filter the movie list by category

 

In the Movie Controller where we will retrieve the Query String:

public ActionResult Index(int CategoryId)
{

return View(new GetMoviesByCategory(CategoryId);  //replace text in brackets with own methods or preferences.

}

P.S Make sure Parameters are the same (CategoryId) in both View and Controller. You can also pass and assign a parameter manually by replacing item.Id with the CategoryId for instance:

new { CategoryId = 5 }