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

 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)结果

2.2  ViewData传递数据

(1)DB表:

(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)结果

2.3  ViewBag传递数据

(1)DB表:

(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)结果

2.4  TempData传递数据

(1)DB表:

(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)结果

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:

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

2.6 ViewBag=》ViewData

2.7 ViewData=》ViewBag

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:

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:

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:

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:

3.5 自定义模型绑定

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

4   版权

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

【ASP.NET MVC】View与Controller之间传递数据的更多相关文章

  1. 【MVC架构】——怎样利用Json在View和Controller之间传递数据

    在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...

  2. Controller之间传递数据:属性传值

    在项目中,Controller之间传递数据非常之多,这里简单介绍一下属性传值.例如有FirstController 和 SecondController,数据从First传递到Second中,我们如何 ...

  3. Controller之间传递数据:Block传值

    http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...

  4. MVC进阶学习--View和Controller之间的数据传递(一)

    1.使用ViewData ViewData 的是ControllerBase 的一个属性,是一个数据字典类型的,其实现代码如(这段代码来自asp.net MVC开源项目中源码)下: Code   1  ...

  5. ASP.NET MVC View向Controller提交数据

    我们知道使用MVC的一个很重的的用途就是把Controller和View之间进行解耦,通过控制器来调用不同的视图,这就注定了Controller和View之间的传值是一个很重的知识点,这篇博文主要解释 ...

  6. ASP.NET MVC系列:从Controller访问Model数据

    在项目解决方案中,添加一个MoviesController控制器,选择对应的模板,和模型类以及数据上下文:关于如何添加模型类和数据上下文,我们在ASP.NET MVC系列:添加模型中已经介绍过

  7. Controller之间传递数据:协议传值

    http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...

  8. MVC进阶学习--View和Controller之间的数据传递(二)

    1. 使用Request.Form MVC 将页面简单化,与WebForm中的事件机制完全不同,就和普通的html标签表单提交没有任何区别(当然WebForm中的事件机制其实也是表单提交).在表单提交 ...

  9. ASP.NET MVC View向Controller传值方式总结

    1:QueryString传值1)也可以使用new{}来为form的action增加querystring2)在controler里使用Request.QueryString["word&q ...

随机推荐

  1. centos6.8 静默安装 oracle 11.2.0.4

    安装环境及系统要求    (下文具体参数值与路径根据自己的环境调整)操作系统:Red Hat Enterprise Linux 6 (x86) 或者CentOS 6 (x64) 数据库:Oracle ...

  2. 今天提示MyEclipse Trial Expired,如何手动获取MyEclipse 注册码!很牛!

    1.建立JAVA Project,随便命名,只要符合规则就行. 2.在刚刚建好的Project右击src,新建一个类,命名为MyEclipseGen,把.java里本来有的代码全部删掉,再把下面的代码 ...

  3. 单双引号的区别,defined容易疏忽的小地方

    单双引号的区别(面试题)                    1.双引号可以解析变量,单引号不行                    2.双引号解析转义字符,单引号不解析转义字符.但是单引号能解析 ...

  4. Java 简单的 socket 编程入门实战

    这个是给女朋友写的:) 首先需要知道我们每个电脑都可以成为server(服务器) 和 client(客户端) 我们需要使用java来实现客户端与服务器的数据传输 先帖上代码 注意这里两张代码处于两个j ...

  5. [NOIP 2011]聪明的质监员

    聪明的质监员 题目 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从 1 到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检验矿产的流程是: 1. 给定 m个区间[ ...

  6. C++ STL vector详解

    一.解释:  vector(向量):是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷,它 ...

  7. OOP in Javascript

    写了几篇Vue入门的内容了,今天写点其它的放松一下,简单讲讲javascript中的面相对象. 在面向对象的语言中,都有类的概念,当然es6中开始javascript中也有类的概念了,这里以es5为基 ...

  8. hdu--1077--Catching Fish

    思路:   1.枚举两点确定圆心,大于2不用考虑   2.逐个判断判断距圆心的距离小于1.00001符合题意 这个题,主要在求圆心上废了不少功夫,但是仍存在问题 #include<iostrea ...

  9. cve-2017-8464 复现 快捷方式远程代码执行

    cve-2017-8464 2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快捷方式时存在远程执行任意代码的高危漏洞,黑客可以通过U盘.网 ...

  10. setTimeout 与 Event Loop 浅析

    先从一个小题目开始: 以下代码的输出结果是? function test1 () { console.log(1) }; setTimeout(test1, 1000); // T1-1setTime ...