如何在Mvc控制器中添加操作结果 [英] How to add the action result in the Mvc controller

查看:69
本文介绍了如何在Mvc控制器中添加操作结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Movie.cs类中有以下代码。我需要帮助来创建动作结果。我的代码是:



 命名空间 MvcMovie.Models 
{
public class 电影
{
public int ID { get ; set ; }
public string 标题{ get ; set ; }
public DateTime ReleaseDate { get ; set ; }
public string 类型{ get ; set ; }
public decimal 价格{ get ; set ; }
}
public class MovieDBContext:DbContext
{
public 列表<电影> 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 *来自电影,con);

SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
con.Close();


foreach (DataRow r in dt.Rows)
{
电影mv = 电影();
mv.ID = Convert.ToInt32(r [ ID]);
mv.Title = r [ 标题]。ToString();
mv.Price = Convert.ToInt32(r [ Price]);
mv.ReleaseDate = Convert.ToDateTime(r [ ReleaseDate]);
mv.Genre = r [ 类型]。ToString();
lmovie.Add(mv);

}
return lmovie;
}

}
}





我需要创建动作结果MoviesController:



  public  ActionResult Index()
{


return 查看(-----); b $ bnumerable< movie>类型,如下所示



 <  ![CDATA [<%@     Control    语言  =  C#   继承  =  System.Web.Mvc.ViewUserControl< System.Collections.Generic.IEnumerable< Movie>>    >  
<! - - 现在编写html标记以显示表格数据 - > ;




控制器写入
下面的代码



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

return 查看(电影);


}


I have the following code in my Movie.cs class. I need help with creating the action result.My code is:

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:

public ActionResult Index()
{
    

    return View(-----);


}

解决方案

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

<![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

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

    return View(movies);


}


这篇关于如何在Mvc控制器中添加操作结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆