Simsons
Simsons

Reputation: 12745

How to Pass Data between Views

I have my Controller which pass Data from to view as below:

IEnumerable<MovieDetails> jsonData = default(IEnumerable<MovieDetails>);

#region Constructor Request to get all Data
public MoviesController()
{
    jsonData = GetJsonValues(URL);
}
public ActionResult Index()
{
    return View(jsonData);
}

Now my view is rendering the controls as :

@model IEnumerable<VikiMVC.Models.MovieDetails>

@foreach (var movieDetails in Model)
{
    <!--Deleted Divs for Simplicity -->
    <img src = @movieDetails.Thumbnail alt = @movieDetails.Thumbnail 
          class="imgStyle"onclick="playMovie(this)" [email protected]/>     
}

Now on click of Image I want to open up another view and access the @movieDetails related to that particular item.

My Movie Details class goes like :

public class MovieDetails
{
   public string Title { get; set; }
   public string Thumbnail { get; set; }
   // public string URI { get; set; }
   public string Description { get; set; }
   public string MovieURI { get; set; }
   // public List<Response> Response { get; set; }
}

SO when I click on an Image I should be able access the MovieURI specific to that particular image. We can do that using querystring , but Razor must be having some thing better.

Upvotes: 3

Views: 860

Answers (3)

lahsrah
lahsrah

Reputation: 9173

What is wrong with passing via QueryString?

I would either pass the MovieURI or the MovieDetails ID via QueryString and then pass that into the View you want to show.

Razr is a View engine. It doesn't change the way you pass data between views. I would avoid using TempData like suggested by others unless I absolutely cannot do it any other way.

Upvotes: 1

karaxuna
karaxuna

Reputation: 26930

Why do you have alt on image twice? Try this:

@foreach (var movieDetails in Model)
{
    <!--Deleted Divs for Simplicity -->
    <img src = @movieDetails.Thumbnail class="imgStyle" onclick="playMovie('@movieDetails.MovieURI')" [email protected]/>     
}

In js:

function playMovie(uri){
    window.location.href = uri;
}

But I would suggest something like this (add Id field in model):

@foreach (var movieDetails in Model)
{
    <!--Deleted Divs for Simplicity -->
    <a href="/Mycontroller/Movie/@movieDetails.Id">
        <img src = @movieDetails.Thumbnail class="imgStyle" [email protected]/>   
    </a>  
}

Action:

public ActionResult Movie(int id)
{
    var movieModel = context.Movies.GetById(id);
    return View(movieModel);
}

Upvotes: 2

SMK
SMK

Reputation: 2158

you could use TempData for passing your data between the different views. Take a look at this link

Upvotes: 0

Related Questions