ASP.NET MVC系列文章
【02】浅谈Google Chrome浏览器(操作篇)(上)
【03】浅谈Google Chrome浏览器(操作篇)(下)
【04】浅谈ASP.NET框架
【07】浅谈ASP.NET MVC 路由
【08】浅谈ASP.NET MVC 视图
【10】浅谈jqGrid 在ASP.NET MVC中增删改查
【13】浅谈NuGet在VS中的运用
【14】浅谈ASP.NET 程序发布过程
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系列】浅谈ASP.NET MVC 视图与控制器传递数据的更多相关文章
-
【ASP.NET MVC系列】浅谈ASP.NET MVC八大类扩展(上篇)
lASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操 ...
-
【ASP.NET MVC系列】浅谈ASP.NET 页面之间传值的几种方式
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
【ASP.NET MVC系列】浅谈ASP.NET MVC运行过程
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
【ASP.NET MVC系列】浅谈ASP.NET MVC 视图
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
【ASP.NET MVC系列】浅谈ASP.NET MVC 控制器
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
【ASP.NET MVC系列】浅谈ASP.NET MVC 路由
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
-
浅谈ASP.NET ---- 系列文章
[01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作篇)(下) [04]浅谈ASP. ...
-
MVC模式浅谈
MVC模式浅谈 一.MVC模式概述 模型-视图-控制器(MVC模式)是一种非常经典的软件架构模式,在UI框架和UI设计思路中扮演着非常重要的角色.从设计模式的角度来看,MVC模式是 一种复合模式,它将 ...
随机推荐
-
AspNetPager控件报错误: Syntax error, unrecognized expression: input#ctl00$ContentPlaceHolder1$Aspnetpager1_input问题解决[摘]
高版本IE,如IE10或者IE11在浏览页面时出现错误: Syntax error, unrecognized expression: input#ctl00$ContentPlaceHolder1$ ...
-
mongoose学习笔记1--基础知识2
Schema简述 Schema —— 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力,仅仅只是数据库模型在程序片段中的一种表现,可以说是数据属性模型(传统 ...
-
haproxy配置文件简单管理
版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...
-
Shell脚本[运算表达式,条件控制语句]
#!/bin/bash #你值得收藏的四则表达式运算. val1=1 val2=1 val3=1 val4=1 val5=1 val6=1 val7=1 let val1++ ((val2++)) v ...
-
mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. ->
mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. -> TEST通过没有报错,但是最终部署到Nexus中时出现错误. 后 ...
-
mysql在linux上的一点操作
1,查看打开端口. show variables like 'port'; 2, 指定ip,用户名,密码 1 grant all privileges on *.* to root@"% ...
-
Android空指针异常的常见情况
把我经常遇到的nullpoitException写在这里,以便以后自己查找原因. 1.用findViewById(param )的方法获取一个view对象的时候,有的时候其实应该是获取一个layout ...
-
Linux命令之_Cut(转)
linux之cut用法 cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut [-bn ...
-
JMeter 的调式工具
任何的编程工具都会相应的调式工具,JMeter的调式 工具主要有五种: 1.查看结果树:含请求信息.响应信息等 2.HTTP 镜像服务器:HTTP Mirror Server用于查看请求信息 3.De ...
-
Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263 git 路径 https://github.com/wangpanjun/d ...