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 }

LINQ for Nhibernate

References Required:

  • using Domain;   //this is the folder where your entities are found
  • using NHibernate;
  • using NHibernate.Criterion;

Other Requirements:

  • Nhibernate / Fluent Nhibernate
  • Linq for Nhibernate
  • Nhibernate Session Helper (can find ready ones on the internet)

1. Return a row by integer Id

public Movies GetById(intId)
{

using(ISession session = NHibernateHelper.OpenSession())

return session.Get<Icon>(Id);

}

2. Return a simple IList<> / Return all rows in a table:
public IList<Movies> GetAll()

{
using(ISession session = NHibernateHelper.OpenSession())

return session.CreateCriteria(typeof(Movies)).List<Movies>();

}

3. Return an IList<> with a WHERE clause:

public IList<Movies> GetMoviesByCategory(int CatId)

{
using(ISession session = NHibernateHelper.OpenSession())

return session.CreateCriteria(typeof(Movies)).Add(Restrictions.Eq(“CategoryId”, CatId)).List<Movies>();

}

4. Return an IList<> with a 3-way JOIN and WHERE clause

public IList<MovieImages> GetMovieImagesByMovieCategory(int CatId)
{

IList<Movie> movies = GetAllMovies(); //Change with your query
IList<MovieCategory> categories = GetAllCategories(); //Change with your query
IList<MovieImages> images = GetAllImages(); //Change with your query

using(ISession session = NHibernateHelper.OpenSession())

return (from mi in images
join m in movies on mi.Movie.Id equals m.Id
join c in categories on m.CategoryId.Id equals c.Id
where c.Id == category.Id
select mi).ToList<MovieImages>();

}

5.Convert List to IList<>

IList<MovieImages> list = myNormalList.ToList<MovieImages>(); //myNormalList would be a common List()