p.Doug p.Doug - 3 months ago 9
ASP.NET (C#) Question

Passing a Variable Between Views

I have a view that pulls data from a model and lists it as a set of links:

<p>@foreach (var genre in Model)</P

a href="@Url.Action("Index", "FullMovieList")"> class="genreItem">@Html.DisplayFor(modelItem => genre.GenreName)


My question is: how do I tell which genre has been clicked on the next page? Should i send the variable:

view -> genre controller -> fulllist controller -> view

and if so, how?

Answer

You can pass the genre in the querystring.

Use this overload of Url.Action

@Url.Action("Index", "FullMovieList",new { genre = genre.GenreName  })

This will generate the url value with querystring like "/FullMovieList/Index?genre=someName"

Assuming your Index action method accepts a parameter named genre and check the value of that and return a relevant response according to that.

public ActionResult Index(string genre="")
{
  if(!String.IsNullOrEmpty(genre))
  {

  }
  // to do  :Return something
}
Comments