• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【ASP.NET MVC系列】浅谈ASP.NET MVC 视图与控制器传递数据

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

ASP.NET MVC系列文章

【01】浅谈Google Chrome浏览器(理论篇)

【02】浅谈Google Chrome浏览器(操作篇)(上)

【03】浅谈Google Chrome浏览器(操作篇)(下)

【04】浅谈ASP.NET框架   

【05】浅谈ASP.NET MVC运行过程    

【06】浅谈ASP.NET MVC 控制器   

【07】浅谈ASP.NET MVC 路由   

【08】浅谈ASP.NET MVC 视图 

【09】浅谈ASP.NET MVC 视图与控制器传递数据

【10】浅谈jqGrid 在ASP.NET MVC中增删改查     

【11】浅谈ASP.NET 页面之间传值的几种方式

【12】浅谈缓存技术在ASP.NET中的运用       

【13】浅谈NuGet在VS中的运用      

【14】浅谈ASP.NET 程序发布过程           

【15】浅谈数据注解和验证           

【16】浅谈依赖注入

【17】浅谈表单和HTML辅助方法

【18】浅谈基于APS.NET身份验证

【19】浅谈ASP.NET MVC 模型

【20】浅谈ASP.NET MVC 单元测试

【21】浅谈ASP.NET MVC网络安全;

【22】浅谈ASP.NET MVC八大类扩展

【23】再谈ASP.NET MVC Routing

【24】浅谈ASP.NET 高级话题

【25】浅谈大型ASP.NET MVC项目(含DEMO)

【26】下一系列:ASP.NET WebAPI


 

1   概述

本篇文章主要从操作上简要分析Controller<=>View之间相互传值,关于页面之间传值,如果感兴趣,可参考我另外一篇文章ASP.NET 页面之间传值的几种方式 。

Controller=》View:Model,ViewBag,ViewData,TempData,ViewBag=>ViewData,ViewData=>ViewBag,ViewModel,JqGrid,AJAX+第三方插件等;

View=》Controller:QueryString,Form,FormCollection,Ajax,自定义模型绑定等;

2   Controller向View传递数据

2.1  Model传递数据

(1)DB表:

(2)Model

 1 public class CustomerInfo
 2     {
 3         public string EmployeeID { get; set; }
 4         public string EmployeeName { get; set; }
 5         public string EmployeeMajor { get; set; }
 6         public string EmployeeDepartment { get; set; }
 7         public string EmployeeTel { get; set; }
 8         public string EmployeeEmail { get; set; }
 9         public string EmployeeJiGuan { get; set; }
10         public string EmployeeAddress { get; set; }
11         public string EmployeePosition { get; set; }
12         public string EmployeeBirthday { get; set; }
13     }

(3)Controller

a.控制器action

public ActionResult ModelDataToView()
        {
            //定义集合
            List<CustomerInfo> ltPI = new List<CustomerInfo>();
            DataTable dt = GetCustomerInfoToDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                CustomerInfo custInfo = new CustomerInfo();
                custInfo.EmployeeID = dt.Rows[i]["EmployeeID"].ToString();
                custInfo.EmployeeName = dt.Rows[i]["EmployeeName"].ToString();
                custInfo.EmployeeMajor = dt.Rows[i]["EmployeeMajor"].ToString();
                custInfo.EmployeeDepartment = dt.Rows[i]["EmployeeDepartment"].ToString();
                custInfo.EmployeeTel = dt.Rows[i]["EmployeeTel"].ToString();
                custInfo.EmployeeEmail = dt.Rows[i]["EmployeeEmail"].ToString();
                custInfo.EmployeeJiGuan = dt.Rows[i]["EmployeeJiGuan"].ToString();
                custInfo.EmployeeAddress = dt.Rows[i]["EmployeeAddress"].ToString();
                custInfo.EmployeePosition = dt.Rows[i]["EmployeePosition"].ToString();
                custInfo.EmployeeBirthday = dt.Rows[i]["EmployeeBirthday"].ToString();
                ltPI.Add(custInfo);
            }
          return View("Index",ltPI);
        }

b.ADO.NET 获取CustomerInfo数据

 1 //获取用户实体
 2         public DataTable GetCustomerInfoToDataTable()
 3         {
 4             //连接字符串
 5             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
 6             string strSql = @"SELECT * FROM EmployeeInfo";
 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();
14                 sda.Fill(ds,"CustomerInfo");
15                 return ds.Tables["CustomerInfo"];
16             }
17         }

(4)View

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
11     <link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
12     <link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
13     <script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
14     <script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
15     <title>Index</title>
16 </head>
17 <body>
18     <div class="table-responsive">
19        <table class="table table-striped">
20                 <thead>
21                    <tr>
22                         <td>员工ID</td>
23                         <td>员工姓名</td>
24                         <td>员工专业</td>
25                         <td>员工部门</td>
26                         <td>员工电话</td>
27                         <td>员工邮件</td>
28                         <td>员工籍贯</td>
29                         <td>员工住址</td>
30                         <td>员工职位</td>
31                         <td>员工生日</td>
32                     </tr>
33                 </thead>
34                 <tbody>
35                     @foreach (var item in Model)
36                     {
37                         <tr>
38                             <td>@item.EmployeeID</td>
39                             <td>@item.EmployeeName</td>
40                             <td>@item.EmployeeMajor</td>
41                             <td>@item.EmployeeDepartment</td>
42                             <td>@item.EmployeeTel</td>
43                             <td>@item.EmployeeEmail</td>
44                             <td>@item.EmployeeJiGuan</td>
45                             <td>@item.EmployeeAddress</td>
46                             <td>@item.EmployeePosition</td>
47                             <td>@item.EmployeeBirthday</td>
48                         </tr>
49                     }
50                 </tbody>
52             </table>
53    </div>
54 </body>
55 </html>

(5)结果

2.2  ViewData传递数据

(1)DB表:

(2)Model

 1 public class CustomerInfo
 2     {
 3         public string EmployeeID { get; set; }
 4         public string EmployeeName { get; set; }
 5         public string EmployeeMajor { get; set; }
 6         public string EmployeeDepartment { get; set; }
 7         public string EmployeeTel { get; set; }
 8         public string EmployeeEmail { get; set; }
 9         public string EmployeeJiGuan { get; set; }
10         public string EmployeeAddress { get; set; }
11         public string EmployeePosition { get; set; }
12         public string EmployeeBirthday { get; set; }
13     }

(3)Controller

a.控制器action

 1  //ViewData传递
 2         public 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();
 9                 custInfo.EmployeeID = dt.Rows[i]["EmployeeID"].ToString();
10                 custInfo.EmployeeName = dt.Rows[i]["EmployeeName"].ToString();
11                 custInfo.EmployeeMajor = dt.Rows[i]["EmployeeMajor"].ToString();
12                 custInfo.EmployeeDepartment = dt.Rows[i]["EmployeeDepartment"].ToString();
13                 custInfo.EmployeeTel = dt.Rows[i]["EmployeeTel"].ToString();
14                 custInfo.EmployeeEmail = dt.Rows[i]["EmployeeEmail"].ToString();
15                 custInfo.EmployeeJiGuan = dt.Rows[i]["EmployeeJiGuan"].ToString();
16                 custInfo.EmployeeAddress = dt.Rows[i]["EmployeeAddress"].ToString();
17                 custInfo.EmployeePosition = dt.Rows[i]["EmployeePosition"].ToString();
18                 custInfo.EmployeeBirthday = dt.Rows[i]["EmployeeBirthday"].ToString();
19                 ltPI.Add(custInfo);
20                 ViewData["CustomerInfo"] = ltPI;
21             }
22             return View();
23         }

 b.ADO.NET 获取CustomerInfo数据

 1 //获取用户实体
 2         public DataTable GetCustomerInfoToDataTable()
 3         {
 4             //连接字符串
 5             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
 6             string strSql = @"SELECT * FROM EmployeeInfo";
 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();
14                 sda.Fill(ds,"CustomerInfo");
15                 return ds.Tables["CustomerInfo"];
16             }
17         }

(4)View

 1 @using MVCCrud.Areas.JqGridDemo.Models
 2 
 3 
 4 @{
 5     Layout = null;
 6 }
 7 
 8 <!DOCTYPE html>
 9 
10 <html>
11 <head>
12     <meta name="viewport" content="width=device-width" />
13     <link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
14     <link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
15     <link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
16     <script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
17     <script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
18     <title>ViewDataToView</title>
19 </head>
20 <body>
21     <div class="table-responsive">
22         <table class="table table-striped">
23             <thead>
24                 <tr>
25                     <td>员工ID</td>
26                     <td>员工姓名</td>
27                     <td>员工专业</td>
28                     <td>员工部门</td>
29                     <td>员工电话</td>
30                     <td>员工邮件</td>
31                     <td>员工籍贯</td>
32                     <td>员工住址</td>
33                     <td>员工职位</td>
34                     <td>员工生日</td>
35                 </tr>
36             </thead>
37             <tbody>
38                 @foreach (var item in (List<CustomerInfo>)ViewData["CustomerInfo"])
39                 {
40                     <tr>
41                         <td>@item.EmployeeID</td>
42                         <td>@item.EmployeeName</td>
43                         <td>@item.EmployeeMajor</td>
44                         <td>@item.EmployeeDepartment</td>
45                         <td>@item.EmployeeTel</td>
46                         <td>@item.EmployeeEmail</td>
47                         <td>@item.EmployeeJiGuan</td>
48                         <td>@item.EmployeeAddress</td>
49                         <td>@item.EmployeePosition</td>
50                         <td>@item.EmployeeBirthday</td>
51                     </tr>
52                 }
53 
54             </tbody>
55         </table>
56     </div>
57 </body>
58 </html>

(5)结果

2.3  ViewBag传递数据

(1)DB表: 

(2)Model

 1 public class CustomerInfo
 2     {
 3         public string EmployeeID { get; set; }
 4         public string EmployeeName { get; set; }
 5         public string EmployeeMajor { get; set; }
 6         public string EmployeeDepartment { get; set; }
 7         public string EmployeeTel { get; set; }
 8         public string EmployeeEmail {  
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap