How do I display data from multiple tables in a single MVC view

前端 未结 3 1365
轮回少年
轮回少年 2020-12-16 03:10

I am having a hard time solving the following with an MVC view.

My goal is to display data from multiple tables in a single MVC view. The bulk of the data comes from

3条回答
  •  囚心锁ツ
    2020-12-16 03:42

    It is simple just do what I say step by step.

    1. add connection string into web.config file

    2. select models from solution explorer and add 4 classes as following

      • 1st class for first table "i have employ table which have 3 columns

        public class Employ { [Key] public int Emp_id { get; set; } public string Emp_name { get; set; } public string Emp_city { get; set; } }

      • 2nd class for my tempo table

        public class tempo { [Key] public int ID { get; set; } public int Emp_Id { get; set; } public string subject { get; set; } public string hobby { get; set; } }

      Now I create a third class in model folder which contain value that i want from employ table and tempo table

      public class Alladd
      {
          public int ID { get; set; }
          public int Emp_Id { get; set; }
          public string subject { get; set; }
          public string hobby { get; set; }
          public string Emp_name { get; set; }
          public string Emp_city { get; set; }
      }  
      

      and the final class is datacontext class

      public class DataContext:DbContext
      {
          public DataContext() : base("DefaultConn")//connection string
          {
          }
      
          public DbSet Empdata { get; set; }
          public DbSet Tempdata { get; set; }
      }
      
    3. now go to the Home controller and add code as below

      public ActionResult file()
      {
          // IList tempi=new List();
      
          IEnumerable model = null;
      
          // model = getVerifydetails(id);
          // return View(objcpModel);
          List verify = new List();
      
          cn.Open();
      
          if (cn.State == ConnectionState.Open)
          {
              string query = "select Employ.Emp_name,Employ.Emp_id,Employ.Emp_city,tempo.hobby,tempo.id,tempo.subject from Employ inner join tempo on Employ.Emp_id=tempo.Emp_id;";//joining two table 
              SqlCommand cmd=new SqlCommand(query,cn);
              SqlDataReader dr = cmd.ExecuteReader();
      
              while (dr.Read())
              {
                  verify.Add(new Alladd { Emp_name = dr[0].ToString(), Emp_Id= Convert.ToInt32(dr[1].ToString()), Emp_city = dr[2].ToString(), hobby = dr[3].ToString(),ID = Convert.ToInt32(dr[1].ToString()),subject= dr[4].ToString()});//filling values into Alladd class
              }
      
              cn.Close();
          }
      
          return View(verify);
      }
      
    4. now the final step is so simple

      1. go to solution explorer
      2. select views folder and left click on it and select add view
      3. now name it as "file" which we give it into controller
      4. check on create strongly type view
      5. select model class from dropdown-> Alladd
      6. select scaffold templet ->List
      7. hit Add button

    Now you're done

    Happy coding...

提交回复
热议问题