[RESOLVED]display data from array instead of EF

Hi, I am new to MVC and trying to display array data in the controller. here is my controller and view. anything I got do on the view side? I just got this result:

System.Collections.Generic.List`1[MVC5.Models.Movie] MVC5.Models.Movie

<div>

This is the view

</div>

Controller:

        public ActionResult Index() 
        { 
            return View(mdb.Movies.ToList()); 
        }

        Movie[] movie = new Movie[] 
        { 
            new Movie { ID = 1, Title = "Tomato Soup", ReleaseDate = DateTime.Now, Genre="action", Price = 1 }, 
            new Movie { ID = 1, Title = "Tomato Soup", ReleaseDate = DateTime.Now, Genre="action", Price = 1 }, 
            new Movie { ID = 1, Title = "Tomato Soup", ReleaseDate = DateTime.Now, Genre="action", Price = 1 } 
        };

        public IEnumerable<Movie> GetMovies()
        {
            return movie.ToList();
        }
@Model MVC5.Models.Movie

@{
    Layout = null;
}

@{
    ViewBag.Title = "index";
 }
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div>
     <p>This is the view</p>

    </div>
</body>
</html>

 

The @model directive needs to be the same type as the data you’re passing

@Model List<MVC5.Models.Movie>

In your view you’d then

@foreach(MVC5.Models.Movie movie in Model)
{
    <p>@movie.Title</p>
}

Hi, AdyF,

 Thanks. It worked. But it displays "System.Collections.Generic.List`1[MVC5.Models.Movie] List" before the result.  how do we get rid of that ?

Hi sdnd,

Thanks for your post.

sdnd2000

"System.Collections.Generic.List`1[MVC5.Models.Movie] List" before the result

change this:

@Model List<MVC5.Models.Movie>

into

@Model IEnumerable<MVC5.Models.Movie>

Hope this can be helpful.

Best Regards,

Eileen

sdnd2000

Hi, AdyF,

 Thanks. It worked. But it displays "System.Collections.Generic.List`1[MVC5.Models.Movie] List" before the result.  how do we get rid of that ?

You’re writing an object’s string representation to the page somewhere, but you haven’t posted your view code so it’s hard to say, but somewhere you’ll have code like

@foreach (Movie m in Model)
{
    @m
}

or

@Model

When you try and show an object as a string it calls ToString and if you haven’t overridden ToString then it shows the typename of the object which is what you’re seeing.

Leave a Reply