Swati Sinha Swati Sinha - 1 year ago 53
ASP.NET (C#) Question

the results of my foreach loop in my browse view is not displaying asp.net mvc

My aim is to display in the (browse View) all movies that are has a status as "recent" appear however not when loading the website not movies that has a status of "recent" appear.
Tried to create a LINQ query in action method to produce a list of movies that has status of "recent".

namespace Cinema1.Models
[Bind(Exclude = "MovieId")]
public class Movie
public int MovieId { get; set; }
public int MovieGenreId { get; set; }
public int MovieStatusId { get; set; }

public virtual MovieStatus MovieStatus { get; set; }
public virtual ICollection<MovieGenre> MovieGenre { get; set; }

//model class for movie status

namespace Cinema1.Models
public class MovieStatus

public int MovieStatusId { get; set; }
public string Status { get; set; }

//Store Controller

public ActionResult Browse(string q)

var statusModel = storeDB.Movies
.Where(a => a.Title.Contains(q))

return View(statusModel.ToList());

//seed database

Namespace Cinema1.Models
public class CinemaInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<CinemaContext>
protected override void Seed(CinemaContext context)
CinemaContext storeDB = new CinemaContext();

context.Movies.Add(new Movie
MovieId = 01,
Title = "Blair Witch",
Rating = 15,
MovieStatusId = 2,
MovieStatus = new MovieStatus { Status = "Recent" },
Cast = "Corbin Reid, Wes Robinson, Valorie Curry",
Director = "Adam Wingard",
RunningTimeMins = 89,
Language = "English",
Synopsis = "It’s been 20 years since James’ sister disappeared into the Black Hills Forest.
MovieDates.ForEach(s => context.MovieDates.Add(s));

//the View for Browse action controller

@model IEnumerable<Cinema1.Models.Movie>

ViewBag.Title = "Browse";

<h2>Movie status</h2>

Select from @Model.Count() Status:

@foreach (var Movies in Model)

Answer Source

My aim is to display in the (browse) all movies that are catagorized/has a status as "recent".At the moment in my view the @model.count() works...output is zero. However should it not be one since one movie has a status on recent?

In your Browse action method you do not have the code to get the movies with status "recent". Instead you have code to get a subset of movies which has the word you pass in querystring "q" in the Movie Title! Why do you expect that code to return movies with status "recent" ???

If you want to get movies with status "Recent", you should have a where clause which checks that. The below code should do that.

public ActionResult Browse()
    var statusModel = storeDB.Movies.Include("MovieStatus")
                                    .Where(g => g.MovieStatus.Name == "Recent");           

    return View(statusModel.ToList());

Now this is going to give you always the movies with "Recent" status. If you want this to be dynamic based on the querystring, you can add a parameter to this method which takes a status name. You can set the default value to "Recent" so that even if you do not pass a status name in querystring, by default, it will use the status parameter value as "Recent"

public ActionResult Browse(string status="Recent")
    var statusModel = storeDB.Movies.Include("MovieStatus")
                                    .Where(g => g.MovieStatus.Name == status);           

    return View(statusModel.ToList());