Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 388
Simsons Avatar asked Mar 16 '26 02:03

Simsons


2 Answers

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);
}
like image 136
karaxuna Avatar answered Mar 18 '26 16:03

karaxuna


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.

like image 42
lahsrah Avatar answered Mar 18 '26 14:03

lahsrah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!