mvc 前後端如何傳值
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(); } }