Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code in my Movie.cs class. I need help with creating the action result.My code is:

C#
namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }
    public class MovieDBContext : DbContext
    {      
        public  List<Movie> GetMoviesList()
        {
            
            List<Movie> lmovie = new List<Movie>();
       
            SqlConnection con = new SqlConnection("Data Source=dev01;Initial Catalog=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Movies", con);
     
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            con.Close();


            foreach (DataRow r in dt.Rows)
            {
                Movie mv = new Movie();
                mv.ID = Convert.ToInt32( r["ID"]);
                mv.Title = r["Title"].ToString();
                mv.Price = Convert.ToInt32(r["Price"]);
                mv.ReleaseDate = Convert.ToDateTime(r["ReleaseDate"]);
                mv.Genre = r["Genre"].ToString();
                lmovie.Add(mv);

            }
            return lmovie;
        }

    }
}


I need to create the action result in the MoviesController:

C#
public ActionResult Index()
{
    

    return View(-----);


}
Posted
Updated 20-Dec-13 2:37am
v2

1 solution

Create a view of IEnumerable<movie> type, as shown below

HTML
<![CDATA[<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<Movie>>" %>
<!-- Now write html markup to display tabular data -->


in your Controller write below code

C#
public ActionResult Index()
{
 MovieDBContext context = new MovieDBContext();
 IList<movies> movies = context.GetMoviesList()

    return View(movies);


}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900