在.net MVC中显示多个数据库查询

时间:2018-03-03 03:05:26

标签: c# asp.net-mvc

我正在使用asp.net MVC创建我的第一个项目 - 我已成功连接到数据库并在索引页面上显示信息。我的问题是如何在一个索引页面上获得多个查询结果 例如

SELECT student ID,first name,surname FROM STUDENT Notes WHERE student ID = 7

我是否需要为每个查询创建新的控制器/模型,或者需要添加到当前,如果我添加到当前如何执行此操作?下面是我目前在控制器中的代码。

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        //int sNumber = 1;
        List<CustomerModel> customers = new List<CustomerModel>();
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {

            string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.Connection = con;
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new CustomerModel
                        {
                            // CustomerId = Convert.ToInt32(sdr["Student Number"]),
                            Title = sdr["title"].ToString(),
                            Name = sdr["first name"].ToString(),
                            Surname = sdr["surname"].ToString()
                        });
                    }
                }                

                con.Close();
            }
        }

        return View(customers);
    }

3 个答案:

答案 0 :(得分:1)

创建一个包含两个结果集的视图模型,例如学生和商标

public class Result
{

  public Student Student { get; set; }

  public Marks Marks { get; set; }

}

使用适当的数据在控制器/服务中加载/构造此结果视图模型,并将此视图模型传递给视图。 我希望这有帮助!

答案 1 :(得分:0)

您应该创建一个新的ViewModel类,其中包含要在View中显示的所有属性。然后在它之后建模你的视图。

根据您目前提供的属性,该类应如下所示:

public class StudentViewModel {
    public int Id { get; set; }
    public string Title { get; set; }
    public string Name { get; set; }
    public strign Surname { get; set; }
}

然后执行值x属性赋值

string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
List<StudentViewModel> model = new List<StudentViewModel>();

using (MySqlCommand cmd = new MySqlCommand(query)) {
    cmd.Connection = con;
    con.Open();
    using (MySqlDataReader sdr = cmd.ExecuteReader())
    {
        while (sdr.Read())
        {
            model.Add(new StudentViewModel
            {
                Id = Convert.ToInt32(sdr["StudentNumber"]),
                Title = Convert.ToString(sdr["title"]),
                Name = Convert.ToString(sdr["first name"]),
                Surname = Convert.ToString(sdr["surname"])
            });
        }
    }

    con.Close();
}

return View(model);

答案 2 :(得分:0)

您必须创建一个包含要在View中显示的所有属性的新类。

示例:

public class StudentModel {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Name { get; set; }
        public strign Surname { get; set; }
    }
    public class MarkModel {
        public int Id { get; set; }
        public int StudentId { get; set; }
        public int SubjectId { get; set; }
        public int Mark { get; set; }
    }
    public class ResultModel
    {

      public StudentModel Student { get; set; }

      public List<MarkModel> Marks { get; set; }

    }

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            //int sNumber = 1;
           var model= new ResultModel{
              Student = new StudentModel(),
              Marks = new List<MarkModel>();
           }

            string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            using (MySqlConnection con = new MySqlConnection(constr))
            {
                string queryStudent = "SELECT id, title, `first name`, surname FROM `STUDENT` WHERE Id=1";
                using (MySqlCommand cmd = new MySqlCommand(queryStudent))
                {
                    cmd.Connection = con;               
                    using (MySqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {    
                               model.student.Id = Convert.ToInt32(sdr["id"]),
                               model.student.Title = sdr["title"].ToString(),
                               model.student.Name = sdr["first name"].ToString(),
                               model.student.Surname = sdr["surname"].ToString()                     
                        }
                    }               
                }

                string queryMarks = "SELECT Id, `StudentId`, StudentId,Mark FROM `MARK` WHERE StudentId=1";
                using (MySqlCommand cmd = new MySqlCommand(queryMarks))
                {
                    cmd.Connection = con;               
                    using (MySqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            model.Marks.Add(new MarkModel
                            {
                               Id = Convert.ToInt32(sdr["Id"]),
                               StudentId = Convert.ToInt32(sdr["StudentId"]),
                               StudentId = Convert.ToInt32(sdr["StudentId"]),
                               Mark = Convert.ToInt32(sdr["Mark"]),
                            });
                        }
                    }               
                } 
            }

            return View(model);
        }