HTML5技术

【ASP.NET MVC】View与Controller之间传递数据 - Alan_beijing(2)

字号+ 作者:H5之家 来源:H5之家 2017-09-10 08:02 我要评论( )

ActionResult ViewDataToView() 3 { 4 ListCustomerInfo ltPI = new ListCustomerInfo (); 5 DataTable dt = GetCustomerInfoToDataTable(); 6 for ( int i = 0 ; i dt.Rows.Count; i++ ) 7 { 8 CustomerInfo custI

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

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • ASP.NET Core MVC – Tag Helper 组件 - Sweet-Tang

    ASP.NET Core MVC – Tag Helper 组件 - Sweet-Tang

    2017-09-01 17:02

  • ASP.NET Core 使用Cookie验证身份 - Sweet-Tang

    ASP.NET Core 使用Cookie验证身份 - Sweet-Tang

    2017-08-24 17:00

  • 权限管理学习 一、ASP.NET Forms身份认证 - 农码一生

    权限管理学习 一、ASP.NET Forms身份认证 - 农码一生

    2017-08-01 11:02

  • ASP.NET MVC5 怒跨 Linux 平台 - Mr.Tua

    ASP.NET MVC5 怒跨 Linux 平台 - Mr.Tua

    2017-07-26 14:01

网友点评
r