【ASP.NET MVC】View与Controller之间传递数据

时间:2023-03-08 15:43:31
【ASP.NET MVC】View与Controller之间传递数据

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

【ASP.NET MVC】View与Controller之间传递数据


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表:

【ASP.NET MVC】View与Controller之间传递数据

(2)Model

 public class CustomerInfo
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string EmployeeMajor { get; set; }
public string EmployeeDepartment { get; set; }
public string EmployeeTel { get; set; }
public string EmployeeEmail { get; set; }
public string EmployeeJiGuan { get; set; }
public string EmployeeAddress { get; set; }
public string EmployeePosition { get; set; }
public string EmployeeBirthday { get; set; }
}

(3)Controller

a.控制器action

public ActionResult ModelDataToView()
{
//定义集合
List<CustomerInfo> ltPI = new List<CustomerInfo>();
DataTable dt = GetCustomerInfoToDataTable();
for (int i = ; 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数据

 //获取用户实体
public DataTable GetCustomerInfoToDataTable()
{
//连接字符串
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string strSql = @"SELECT * FROM EmployeeInfo";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"CustomerInfo");
return ds.Tables["CustomerInfo"];
}
}

(4)View

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
<title>Index</title>
</head>
<body>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<td>员工ID</td>
<td>员工姓名</td>
<td>员工专业</td>
<td>员工部门</td>
<td>员工电话</td>
<td>员工邮件</td>
<td>员工籍贯</td>
<td>员工住址</td>
<td>员工职位</td>
<td>员工生日</td>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.EmployeeID</td>
<td>@item.EmployeeName</td>
<td>@item.EmployeeMajor</td>
<td>@item.EmployeeDepartment</td>
<td>@item.EmployeeTel</td>
<td>@item.EmployeeEmail</td>
<td>@item.EmployeeJiGuan</td>
<td>@item.EmployeeAddress</td>
<td>@item.EmployeePosition</td>
<td>@item.EmployeeBirthday</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>

(5)结果

【ASP.NET MVC】View与Controller之间传递数据

2.2  ViewData传递数据

(1)DB表:

【ASP.NET MVC】View与Controller之间传递数据

(2)Model

 public class CustomerInfo
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string EmployeeMajor { get; set; }
public string EmployeeDepartment { get; set; }
public string EmployeeTel { get; set; }
public string EmployeeEmail { get; set; }
public string EmployeeJiGuan { get; set; }
public string EmployeeAddress { get; set; }
public string EmployeePosition { get; set; }
public string EmployeeBirthday { get; set; }
}

(3)Controller

a.控制器action

  //ViewData传递
public ActionResult ViewDataToView()
{
List<CustomerInfo> ltPI = new List<CustomerInfo>();
DataTable dt = GetCustomerInfoToDataTable();
for (int i = ; 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);
ViewData["CustomerInfo"] = ltPI;
}
return View();
}

b.ADO.NET 获取CustomerInfo数据

 //获取用户实体
public DataTable GetCustomerInfoToDataTable()
{
//连接字符串
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string strSql = @"SELECT * FROM EmployeeInfo";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"CustomerInfo");
return ds.Tables["CustomerInfo"];
}
}

(4)View

 @using MVCCrud.Areas.JqGridDemo.Models

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
<title>ViewDataToView</title>
</head>
<body>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<td>员工ID</td>
<td>员工姓名</td>
<td>员工专业</td>
<td>员工部门</td>
<td>员工电话</td>
<td>员工邮件</td>
<td>员工籍贯</td>
<td>员工住址</td>
<td>员工职位</td>
<td>员工生日</td>
</tr>
</thead>
<tbody>
@foreach (var item in (List<CustomerInfo>)ViewData["CustomerInfo"])
{
<tr>
<td>@item.EmployeeID</td>
<td>@item.EmployeeName</td>
<td>@item.EmployeeMajor</td>
<td>@item.EmployeeDepartment</td>
<td>@item.EmployeeTel</td>
<td>@item.EmployeeEmail</td>
<td>@item.EmployeeJiGuan</td>
<td>@item.EmployeeAddress</td>
<td>@item.EmployeePosition</td>
<td>@item.EmployeeBirthday</td>
</tr>
} </tbody>
</table>
</div>
</body>
</html>

(5)结果

【ASP.NET MVC】View与Controller之间传递数据

2.3  ViewBag传递数据

(1)DB表:

【ASP.NET MVC】View与Controller之间传递数据

(2)Model

 public class CustomerInfo
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string EmployeeMajor { get; set; }
public string EmployeeDepartment { get; set; }
public string EmployeeTel { get; set; }
public string EmployeeEmail { get; set; }
public string EmployeeJiGuan { get; set; }
public string EmployeeAddress { get; set; }
public string EmployeePosition { get; set; }
public string EmployeeBirthday { get; set; }
}

(3)Controller

a.控制器action

 //ViewBag传递
public ActionResult ViewBagDataToView()
{
List<CustomerInfo> ltPI = new List<CustomerInfo>();
DataTable dt = GetCustomerInfoToDataTable();
for (int i = ; 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);
ViewBag.CustomerInfo = ltPI;
}
return View();
}

b.ADO.NET 获取CustomerInfo数据

 //获取用户实体
public DataTable GetCustomerInfoToDataTable()
{
//连接字符串
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string strSql = @"SELECT * FROM EmployeeInfo";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"CustomerInfo");
return ds.Tables["CustomerInfo"];
}
}

(4)View

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
<title>ViewBagDataToView</title>
</head>
<body>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<td>员工ID</td>
<td>员工姓名</td>
<td>员工专业</td>
<td>员工部门</td>
<td>员工电话</td>
<td>员工邮件</td>
<td>员工籍贯</td>
<td>员工住址</td>
<td>员工职位</td>
<td>员工生日</td>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.CustomerInfo)
{
<tr>
@*<td>@item.Em</td>*@
<td>@item.EmployeeName</td>
<td>@item.EmployeeMajor</td>
<td>@item.EmployeeDepartment</td>
<td>@item.EmployeeTel</td>
<td>@item.EmployeeEmail</td>
<td>@item.EmployeeJiGuan</td>
<td>@item.EmployeeAddress</td>
<td>@item.EmployeePosition</td>
<td>@item.EmployeeBirthday</td>
</tr>
} </tbody>
</table> </div>
</body>
</html>

(4)View

 @using MVCCrud.Areas.JqGridDemo.Models

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="~/OuterLibrary/bootstrap/bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<script src="~/OuterLibrary/bootstrap/bootstrap/js/bootstrap.js"></script>
<title>ViewBagDataToView</title>
</head>
<body>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<td>员工ID</td>
<td>员工姓名</td>
<td>员工专业</td>
<td>员工部门</td>
<td>员工电话</td>
<td>员工邮件</td>
<td>员工籍贯</td>
<td>员工住址</td>
<td>员工职位</td>
<td>员工生日</td>
</tr>
</thead>
<tbody>
@foreach (var item in (List<CustomerInfo>)TempData["CustomerInfo"])
{
<tr>
<td>@item.EmployeeID</td>
<td>@item.EmployeeName</td>
<td>@item.EmployeeMajor</td>
<td>@item.EmployeeDepartment</td>
<td>@item.EmployeeTel</td>
<td>@item.EmployeeEmail</td>
<td>@item.EmployeeJiGuan</td>
<td>@item.EmployeeAddress</td>
<td>@item.EmployeePosition</td>
<td>@item.EmployeeBirthday</td>
</tr>
} </tbody>
</table>
</div>
</body>
</html>

(5)结果

【ASP.NET MVC】View与Controller之间传递数据

2.4  TempData传递数据

(1)DB表:

【ASP.NET MVC】View与Controller之间传递数据

(2)Model

  public class CustomerInfo
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string EmployeeMajor { get; set; }
public string EmployeeDepartment { get; set; }
public string EmployeeTel { get; set; }
public string EmployeeEmail { get; set; }
public string EmployeeJiGuan { get; set; }
public string EmployeeAddress { get; set; }
public string EmployeePosition { get; set; }
public string EmployeeBirthday { get; set; }
}

(3)Controller

a.action

  //TempData传递数据
public ActionResult TempDataToView()
{
List<CustomerInfo> ltPI = new List<CustomerInfo>();
DataTable dt = GetCustomerInfoToDataTable();
for (int i = ; 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);
TempData["CustomerInfo"] = ltPI;
}
return View();
}

b.ADO.NET 获取CustomerInfo数据

  //获取用户实体
public DataTable GetCustomerInfoToDataTable()
{
//连接字符串
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string strSql = @"SELECT * FROM EmployeeInfo";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"CustomerInfo");
return ds.Tables["CustomerInfo"];
}
}

(5)结果

【ASP.NET MVC】View与Controller之间传递数据

2.5 第三方插件

JqGrid插件:

控制器:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class JqGridCustomerInfoController : Controller
{
// GET: JqGridDemo/JqGridCustomerInfo
public ActionResult Index()
{
return View();
}
}
}

视图:

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.css" rel="stylesheet" />
<link href="~/OuterLibrary/jquery-ui-themes-1.12.1/jquery-ui.theme.css" rel="stylesheet" />
<link href="~/OuterLibrary/tonytomov-jqGrid-6659334/css/ui.jqgrid.css" rel="stylesheet" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<script src="~/OuterLibrary/tonytomov-jqGrid-6659334/js/i18n/grid.locale-en.js"></script>
<script src="~/OuterLibrary/tonytomov-jqGrid-6659334/js/jquery.jqGrid.js"></script>
<title>Index</title>
</head>
<body>
<div>
<div class="main" id="main">
<table id="JqGrid-table"></table>
<div id="JqGrid-pager"></div>
</div>
<script type="text/javascript">
$("#JqGrid-table").jqGrid({
url: "/JqGridDemo/JsonDemo/Index",
datatype: "json",
height: 150,
mtype: "Get",
colNames: ['员工ID', '员工姓名', '员工专业', '员工部门', '员工电话','员工邮件','员工籍贯','员工住址','员工职位','员工生日'],
colModel: [{
name: 'EmployeeID',
index: 'EmployeeID',
key: true,
width: 100,
editable: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeName',
index: 'EmployeeName',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeMajor',
index: 'EmployeeMajor',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
},
{
name: 'EmployeeDepartment',
index: 'EmployeeDepartment',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeTel',
index: 'EmployeeTel',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeEmail',
index: 'EmployeeEmail',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeJiGuan',
index: 'EmployeeJiGuan',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeAddress',
index: 'EmployeeAddress',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeePosition',
index: 'EmployeePosition',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, {
name: 'EmployeeBirthday',
index: 'EmployeeBirthday',
width: 200,
editable: false,
edittype: false,
editoptions: {
size: "20",
maxlength: "30"
}
}, ],
viewrecords: true,
rowNum: 10,
rowList: [10, 20, 30],
pager: '#JqGrid-pager',
altRows: true,
multiselect: true,
multiboxonly: true,
caption: "员工信息表",
autowidth: true
});
jQuery("#grid-table").jqGrid('navGrid', '#grid-pager', { edit: true, add: true, del: true });
</script>
</div>
</body>
</html>

控制器:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Script.Serialization; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class JsonDemoController : Controller
{
// GET: JqGridDemo/JsonDemo public ActionResult Index()
{
var jsondata = new[]
{
new{
EmployeeID = "NX0001",
EmployeeName = "张三",
EmployeeMajor = "金融学",
EmployeeDepartment = "风投部门",
EmployeeTel = "XXX",
EmployeeEmail="XXX@qq.com",
EmployeeJiGuan="上海",
EmployeeAddress="上海浦东新区",
EmployeePosition="高级软件工程师",
EmployeeBirthday="XXX",
}
};
return Json(jsondata,JsonRequestBehavior.AllowGet);
}
}
}

result:

【ASP.NET MVC】View与Controller之间传递数据

关于第三方插件,类型比较多,如Bootstrap-table等,希望广大读者朋友去研究。JqGrid,其功能很强大,在本篇文章中,仅仅是提及,下篇文章将重点分析JqGrid,与广大读者朋友分享。

2.6 ViewBag=》ViewData

【ASP.NET MVC】View与Controller之间传递数据

2.7 ViewData=》ViewBag

【ASP.NET MVC】View与Controller之间传递数据

2.8 ViewModel

留给读者朋友们去研究。。。。。。

2.9 Ajax+第三方插件(JqGrid,BootStrap-table)

留给读者朋友们去研究。。。。。。

3  View向Controller传递数据

3.1  QueryString

controller:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class QueryStringController : Controller
{
// GET: JqGridDemo/QueryString
public ActionResult Index()
{
return View();
} public void GetParamsFromToView(string EmployeeID,string EmployeeName)
{
EmployeeID = Request["EmployeeID"].ToString();
EmployeeName= Request["EmployeeName"].ToString();
}
}
}

View:

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<title>Index</title>
<script>
$(function () {
$('#btnQueryString').click(function () {
//url不区分大小写
location.href ="/JqGridDemo/QueryString/GetParamsFromToView?EmployeeID=NX001&EmployeeName=张三";
});
});
</script>
<style>
#btnQueryString{
width:320px;
height:30px;
}
</style> </head>
<body>
<div>
<button id="btnQueryString">QueryString向Controller传递值</button>
</div>
</body>
</html>

result:

【ASP.NET MVC】View与Controller之间传递数据

3.2  AJax

controller:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class AjaxDataController : Controller
{
// GET: JqGridDemo/AjaxData
public ActionResult Index()
{
return View();
} //action Receiving data from Ajax
public void GetParamsFromAjax(string EmployeeID, string EmployeeName)
{ }
}
}

View:

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<title>Index</title>
<script>
$(function () {
$('#btnAjax').click(function () {
$.ajax({
url: "/JqGridDemo/AjaxData/GetParamsFromAjax",
type:"GET",
data:{EmployeeID:'NX001',EmployeeName:'张三'},
error: function(message) {
alert('error!');
}
});
})
})
</script>
</head>
<body>
<button id="btnAjax">Ajax传递参数</button>
</body>
</html>

或者

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/OuterLibrary/Jquery/jquery-3.2.1.js"></script>
<title>Index</title>
<script>
$(function () {
$('#btnAjax').click(function () {
$.ajax({
url: "/JqGridDemo/AjaxData/GetParamsFromAjax" +"?EmployeeID='NX001'&EmployeeName='张三",
type:"GET",
//data:{EmployeeID:'NX001',EmployeeName:'张三'},
error: function(message) {
alert('error!');
}
});
})
})
</script>
</head>
<body>
<button id="btnAjax">Ajax传递参数</button>
</body>
</html>

result:

【ASP.NET MVC】View与Controller之间传递数据

3.3  Form传递

controller:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class FormTransferDataController : Controller
{
// GET: JqGridDemo/FormTransferData
public ActionResult Index()
{
return View();
} //action Receiving data from Form
public void GetParamsFromForm(string EmployeeID, string EmployeeName)
{ }
}
}

View:

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form action="/JqGridDemo/FormTransferData/GetParamsFromForm" method="get">
员工ID:<input type="text" name="EmployeeID" />
员工姓名:<input type="text" name="EmployeeName" />
<input type="submit" name="btnFormTransferData" value="Form表单传递数据" />
</form>
</body>
</html>

result:

【ASP.NET MVC】View与Controller之间传递数据

3.4  FormCollection

controller:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVCCrud.Areas.JqGridDemo.Controllers
{
public class FormCollectionTransferDataController : Controller
{
// GET: JqGridDemo/FormCollectionTransferData
public ActionResult Index()
{
return View();
} //action Receiving data from FormCollection
public void GetParamsFromFormCollection(FormCollection fc)
{
string EmployeeID = fc["EmployeeID"].ToString();
string EmployeeName = fc["EmployeeName"].ToString();
}
}
}

view:

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("GetParamsFromFormCollection", "FormCollectionTransferData"))
{
@Html.TextBox("EmployeeID","员工ID");
@Html.TextBox("EmployeeName","员工姓名");
<input type="submit" value="FormCollection传值"/>
}
</div>
</body>
</html>

result:

【ASP.NET MVC】View与Controller之间传递数据

3.5 自定义模型绑定

敬请期待,下篇文章与大家一起分享。。。。

4   版权

  • 感谢您的阅读,若有不足之处,欢迎指教,共同学习、共同进步。
  • 博主网址:http://www.cnblogs.com/wangjiming/。
  • 极少部分文章利用读书、参考、引用、抄袭、复制和粘贴等多种方式整合而成的,大部分为原创。
  • 如您喜欢,麻烦推荐一下;如您有新想法,欢迎提出,邮箱:2016177728@qq.com。
  • 可以转载该博客,但必须著名博客来源。