3

I am trying to return a list to my view. I can return it with @viewbag but i only get one entry from it . If i try to do return view(items) I get exception issues. what should I do ?

here is my controller

   foreach (var movie in item)
        {
            var items = _db.Movies.Where(s => s.imdbID.Equals(movie.Movieid)).ToList();
             ViewBag.Movies = items;
            return View(items);
        }

Here is my view

@foreach (var item in ViewBag.Movies)
{
<p> @item.Title </p>
 

}
1
  • 2
    Is this in ASP.NET Core MVC? Or ASP.NET MVC? I'm assuming the former, but want to verify based on the tags. (If it is regarding ASP.NET Core MVC, you'll want to swap out [asp.net-mvc] for [asp.net-core-mvc]. Though, admittedly, I'm not sure that the distinction really matters for this particular question. Commented Jun 28, 2020 at 23:52

3 Answers 3

3

First of all you must not return View() inside a loop.

What is the meaning there ?

var items = new List<Movies>();

      foreach (var movie in item)
            {
                items = items.Add(_db.Movies.Where(s => 
                                       s.imdbID.Equals(movie.Movieid)).ToList());
            }
    return View(items);

And if you are returning the list why do you need that ViewBag.

ViewBag is needed when you dont want to include any other model in Dto you are passing. So if you are passing list of type Movies and for example you need another list of different type you pass the other one on ViewBag.

Do this on your View:

@model List<Movies>
@foreach(var item in Model)
{
    <p> @item.Title </p>
}
Sign up to request clarification or add additional context in comments.

Comments

1

Move your return outside the loop and remove the ViewBag, it looks like your controller is wrong, and some part of the View. I Would also change the Where to FirstOrDefault, because you expect only 1 Movie to be found in the db. And remove the .ToList(), because that does nothing right now. like this:

var items = new List<Movies>();

foreach (var movie in item)
{
  items.Add(_db.Movies.FirstOrDefault(s => s.imdbID.Equals(movie.Movieid)));
}

return View(items);

Also change your view to this:

@model IEnumerable<namespace.Models.movies>

@foreach(var item in Model)
{
   <p> @item.Title </p>
}

You should use your model in the view, not the Viewbag, because right now, the (items) does nothing.

Also, look up how to add to a list in c#, it feels like you have some more to learn there, you can check here: https://www.jennerstrand.se/add-object-to-list-in-c-sharp/

Comments

0

In view, you have to import the model of movies.

For example in view,

@model IEnumerable<projectName.models.movies>
@foreach(var item in Model)
{
    <p> @item.Title </p>
}

3 Comments

hello. I still get one result though. I believe my problem is the controller not the view
@Demeteor make sure the items has more than one record?
yes it has more than 10items in the record for the same user. I think its the logic inside my controller foreach loop . since it read each item. but it will always replace them with a new one and at the end I will always have the last entry. I need to find a way to make change that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.