mvc 前后端如何交互数据
1、后端如何传送数据给前端
后端:
ViewBag.Message = "Hello " + name;
前端:
@ViewBag.Message
2、后端如何传送数据给前端
后端:
TempData["name"] = "王五";
前端:@TempData["name"]
3、后端如何传送数据给前端
后端:
ViewData["name"] = "王五";
前端:@ViewData["name"]
4、后端如何传送数据给前端
后端:
PersonViewModel model = new PersonViewModel() { Name="赵六", Age=18};
return View(model);
前端:
@using _06Mvc.Models;
@{
var res = "名称:" + Model.Name + ":" + "年龄:" + Model.Age;
}
5、后端如何传送数据给前端
后端:
List<CustomerInfo> ltPI = new List<CustomerInfo>();
return View("Index",ltPI);
前端:@using list...
6、后端如何传送数据给前端
后端:return Json(obj,JsonRequestBehavior.AllowGet);
前端:记得好像是数组
7、前端如何给后端传送数据
前端:
location.href ="/JqGridDemo/QueryString/GetParamsFromToView?EmployeeID=NX001&EmployeeName=张三";
后端:
var EmployeeID = Request["EmployeeID"].ToString();
var EmployeeName= Request["EmployeeName"].ToString();
8、前端如何传值给后端
前端:
$.ajax({
url: "/JqGridDemo/AjaxData/GetParamsFromAjax",
type:"GET",
data:{EmployeeID:‘NX001‘,EmployeeName:‘张三‘},
error: function(message) {
alert(‘error!‘);
}
});
后端:
public class AjaxDataController : Controller
{
// GET: JqGridDemo/AjaxData
public ActionResult Index()
{
return View();
}
//action Receiving data from Ajax
public void GetParamsFromAjax(string EmployeeID, string EmployeeName)
{
}
}
9、前端如何传值给后端
<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>
后端:
public class FormTransferDataController : Controller
{
// GET: JqGridDemo/FormTransferData
public ActionResult Index()
{
return View();
}
//action Receiving data from Form
public void GetParamsFromForm(string EmployeeID, string EmployeeName)
{
}
}
10、前端如何传值给后端
<div>
@using (Html.BeginForm("GetParamsFromFormCollection", "FormCollectionTransferData"))
{
@Html.TextBox("EmployeeID","员工ID");
@Html.TextBox("EmployeeName","员工姓名");
<input type="submit" value="FormCollection传值"/>
}
</div>
后端:
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();
}
}
再一种是:
1.用一下代码括住提交的表单区域
@using(Html.BeginForm("AddMemberBaseInfo", "MemberManagement",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<inputtype="submit" class="buttons1" style="margin:0auto;" value="保存"/>
<inputtype="text" class="texts" name="txtMemberName" />
}
2.js代码
$(document).ready(function () {
$(‘form‘).bind(‘submit‘,checkform);
});
function checkform() {
alert("提交前的验证");
return false;
}
3.控制器照常取值
public ActionResult AddMemberBaseInfo()
{
string memberName=Request.Form["txtMemberName"].ToString();
}
————————————————
版权声明:本文为CSDN博主「moshengrenhere」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/moshengrenhere/article/details/82697402