文章出處
文章列表
前面學習了,從控制器向視圖傳遞數據,現在學習怎么從視圖向控制器傳遞數據。
通常,我們有兩種方式,來處理瀏覽器的請求,一個是GET方式,一個是POST方式。一般來說,直接通過在瀏覽器中輸入URL話,請求的方式是GET,那么GET方式的Action方法將會被調用,另一方面,如果是點擊一個Button提交一個表單的話,這個時候POST方式的Action就會被調用。
這里我們說的是POST方式,怎么從視圖向控制器傳遞數據,即把用戶輸入的表單信息的數據,傳遞到控制器對應的Action方法中。
這里先列舉一下,從視圖向控制器傳遞數據有4種方法:
- 使用傳統的方法【Request】
- 使用FormCollection方式
- 自定義參數的方式
- 模型綁定
先看看,傳統的方式:
1.首先新建一個空白的MVC項目:,新建一個控制器Home
public ActionResult Index() { return View(); } #region 1.傳統的方式:通過Qequest方式獲取從視圖傳遞過來的數據 [HttpPost] public ActionResult PostIndex() { //1.傳統的方式:通過Qequest方式獲取從視圖傳遞過來的數據 //這里的Request["Name"]中的Name是文本框中的name屬性值, //不能在視圖中去掉,否則報錯:未將對象引用設置到對象的實例 //Request不能通過索引來取值,即Request[0] string name = Request["Name"].ToString(); string sex = Request["Sex"].ToString(); int age = Convert.ToInt32(Request["Age"].ToString()); string phone = Request["Phone"].ToString(); StringBuilder sb = new StringBuilder(); sb.Append("<b>Name:" + name + "</b><br/>"); sb.Append("<b>Sex:" + sex + "</b><br/>"); sb.Append("<b>Age:" + age + "</b><br/>"); sb.Append("<b>Phone:" + phone + "</b><br/>"); return Content(sb.ToString()); } #endregion
對應的Index視圖
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style type="text/css"> #parentDIV { border:1px solid green; text-align:center; } </style> </head> <body> <div> @using (Ajax.BeginForm("PostIndex", "Home", new AjaxOptions() { UpdateTargetId = "myDIV", HttpMethod = "POST" })) { <div id="myDIV"></div> <div id="parentDIV"> <div> @*label中的for里面的值是相對應的文本框的ID值*@ <label for="Name">姓名:</label> <input type="text" value="" placeholder="姓名" id="Name" name="Name" /> </div> <div> <label for="Man">性別:</label> @*提交的是值value*@ <input type="radio" name="Sex" id="Man" value="男" />男<input type="radio" name="Sex" id="Female" value="女" />女 </div> <div> <label for="Age">年齡:</label> <input type="text" value="" placeholder="年齡" id="Age" name="Age" /> </div> <div> <label for="Phone">電話:</label> <input type="text" value="" placeholder="電話" id="Phone" name="Phone" /> </div> <div> <button>確定</button> </div> </div> } </div> </body> </html>
運行一下項目:
輸入數據,點擊確定:
這里就是通過傳統方式,從視圖向控制器傳遞數據了。
在看看FormCollection方式:
#region 2.通過FormCollection:和Request類似 [HttpPost] public ActionResult PostIndex(FormCollection formCol) { //2.通過FormCollection:和Request類似 ///不能在視圖中去掉,否則報錯:未將對象引用設置到對象的實例, //并且去掉之后,Name的值就獲取不到了,在參數里面使用formCol[0],獲取到的就是Sex控件的值 //string name = formCol[0].ToString(); string name = formCol["Name"].ToString(); string sex = formCol["Sex"].ToString(); int age = Convert.ToInt32(formCol["Age"].ToString()); string phone = formCol["Phone"].ToString(); StringBuilder sb = new StringBuilder(); sb.Append("<b>Name:" + name + "</b><br/>"); sb.Append("<b>Sex:" + sex + "</b><br/>"); sb.Append("<b>Age:" + age + "</b><br/>"); sb.Append("<b>Phone:" + phone + "</b><br/>"); return Content(sb.ToString()); } #endregion public ActionResult Index() { return View(); }
結果也是一樣的。
在看看自定義參數方式:
#region 3.通過使用自定義參數方式: [HttpPost] public ActionResult PostIndex(string name, string sex, string age, string phone) { //3.通過使用自定義參數方式: //這里的參數的名字name,sex,age,phone, //必須要和視圖中的空間的name屬性值的名字一樣,但大小寫可以無所謂; //因為一般用戶輸入的數據都是字符串類型的,所以我們在使用參數的時候,都定義成string類型,在用的時候在轉化。 //當然也可以定義成其他類型,比如這里的age參數,我可以定義成int? age,也是可以的。 //還有參數的順序無所謂; //string name = formCol[0].ToString(); //string ame = _name; //string sex = formCol["Sex"].ToString(); int _age = Convert.ToInt32(age); //string phone = formCol["Phone"].ToString(); StringBuilder sb = new StringBuilder(); sb.Append("<b>Name:" + name + "</b><br/>"); sb.Append("<b>Sex:" + sex + "</b><br/>"); sb.Append("<b>Age:" + _age + "</b><br/>"); sb.Append("<b>Phone:" + phone + "</b><br/>"); return Content(sb.ToString()); } #endregion public ActionResult Index() { return View(); }
結果一樣。
最后看看模型綁定:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace DataFromVIewToController.Models { public class Student { public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } public string Phone { get; set; } } }
#region 4.模型綁定 [HttpPost] public ActionResult PostIndex(Student model) { //4.通過使用模型綁定的方式: StringBuilder sb = new StringBuilder(); sb.Append("<b>Name:" + model.Name + "</b><br/>"); sb.Append("<b>Sex:" + model.Sex + "</b><br/>"); sb.Append("<b>Age:" + model.Age + "</b><br/>"); sb.Append("<b>Phone:" + model.Phone + "</b><br/>"); return Content(sb.ToString()); } #endregion public ActionResult Index() { return View(); }
@model DataFromVIewToController.Models.Student @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style type="text/css"> #parentDIV { border:1px solid green; text-align:center; } </style> </head> <body> <div> @using (Ajax.BeginForm("PostIndex", "Home", new AjaxOptions() { UpdateTargetId = "myDIV", HttpMethod = "POST" })) { <div id="myDIV"></div> <div id="parentDIV"> <div> @*label中的for里面的值是相對應的文本框的ID值*@ <label for="Name">姓名:</label> @*<input type="text" value="" placeholder="姓名" id="Name" name="Name" />*@ @Html.TextBoxFor(s=>s.Name) </div> <div> <label for="Man">性別:</label> @*提交的是值value*@ @*<input type="radio" name="Sex" id="Man" value="男" />男<input type="radio" name="Sex" id="Female" value="女" />女*@ @Html.RadioButtonFor(s=>s.Sex,"男") 男 @Html.RadioButtonFor(s=>s.Sex,"女") 女 </div> <div> <label for="Age">年齡:</label> @*<input type="text" value="" placeholder="年齡" id="Age" name="Age" />*@ @Html.TextBoxFor(s => s.Age) </div> <div> <label for="Phone">電話:</label> @*<input type="text" value="" placeholder="電話" id="Phone" name="Phone" />*@ @Html.TextBoxFor(s => s.Phone) </div> <div> <button>確定</button> </div> </div> } </div> </body> </html>
結果:
文章列表
全站熱搜