ActionResult ViewDataToView() 3 { 4 List<CustomerInfo> ltPI = new List<CustomerInfo>(); 5 DataTable dt = GetCustomerInfoToDataTable(); 6 for (int i = 0; i < dt.Rows.Count; i++) 7 { 8 CustomerInfo custInfo = new CustomerInfo(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); 19 ltPI.Add(custInfo); ] = ltPI; 21 } 22 return View(); 23 }
b.ADO.NET 获取CustomerInfo数据
DataTable GetCustomerInfoToDataTable() 3 { conStr = ConfigurationManager.ConnectionStrings[].ConnectionString; ; 7 using (SqlConnection conn = new SqlConnection(conStr)) 8 { 9 conn.Open(); 10 SqlCommand cmd = new SqlCommand(strSql, conn); 11 cmd.ExecuteNonQuery(); 12 SqlDataAdapter sda = new SqlDataAdapter(strSql, conn); 13 DataSet ds = new DataSet(); ); ]; 16 } 17 }
(4)View
1 @using MVCCrud.Areas.JqGridDemo.Models @{ 5 Layout = null; 6 } ViewDataToView员工ID员工姓名员工专业员工部门员工电话员工邮件员工籍贯员工住址员工职位员工生日@foreach (var item in (List)ViewData["CustomerInfo"]) 39 { @item.EmployeeID@item.EmployeeName@item.EmployeeMajor@item.EmployeeDepartment@item.EmployeeTel@item.EmployeeEmail@item.EmployeeJiGuan@item.EmployeeAddress@item.EmployeePosition@item.EmployeeBirthday }
(5)结果
2.3 ViewBag传递数据
(1)DB表:
(2)Model
CustomerInfo 2 { EmployeeID { get; set; } EmployeeName { get; set; } EmployeeMajor { get; set; } EmployeeDepartment { get; set; } EmployeeTel { get; set; } EmployeeEmail { get; set; } EmployeeJiGuan { get; set; } EmployeeAddress { get; set; } EmployeePosition { get; set; } EmployeeBirthday { get; set; } 13 }
(3)Controller
a.控制器action
ActionResult ViewBagDataToView() 3 { 4 List<CustomerInfo> ltPI = new List<CustomerInfo>(); 5 DataTable dt = GetCustomerInfoToDataTable(); 6 for (int i = 0; i < dt.Rows.Count; i++) 7 { 8 CustomerInfo custInfo = new CustomerInfo(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); ].ToString(); 19 ltPI.Add(custInfo); 20 ViewBag.CustomerInfo = ltPI; 21 } 22 return View(); 23 }
b.ADO.NET 获取CustomerInfo数据
DataTable GetCustomerInfoToDataTable() 3 { conStr = ConfigurationManager.ConnectionStrings[].ConnectionString; ; 7 using (SqlConnection conn = new SqlConnection(conStr)) 8 { 9 conn.Open(); 10 SqlCommand cmd = new SqlCommand(strSql, conn); 11 cmd.ExecuteNonQuery(); 12 SqlDataAdapter sda = new SqlDataAdapter(strSql, conn); 13 DataSet ds = new DataSet(); ); ]; 16 } 17 }
(4)View