What?
action方法接收浏覽器參數的方式有4種:
1、模型綁定
2、request方法
3、根據url的規則來接收
4、formcollection form
HOW?
請看下面的DEMO
1、模型綁定
效果圖:

背景接收:
代碼:
models:(在這裡我建立了一個student實體)
public class student
{
public string name { get; set; }
public string id { get; set; }
}
界面:
<body>
<form action ="/test/add" method ="post" >
<h2>Elsa's test</h2>
學生ID
<input id="id" type="text" name="id" value="1" />
學生姓名
<input id="name" type="text" name="name" value="Elsa" />
<input id="id" type="submit" value="add" />
</form>
</body>
controller:
[HttpPost]
public ActionResult add(Models.student model)
{
string id= model.id;
string name = model.name;
return null;
}
2、request
post接收:
其他代碼一樣,controller代碼:
[HttpPost]
public ActionResult add()
{
string id = Request.Form["id"];
string name = Request.Form["name"];
return null;
}
get方式:
[HttpGet]
public ActionResult add()
{
string id = Request.QueryString["id"];
string name=Request.QueryString["name"];
return null;
}
3、根據url的規則來接收
controller:
public ActionResult add(string id,string name)
{
string id1 = id;
string name1=name;
return null;
}
4、formcollection form
controller
public ActionResult add(FormCollection form)
{
string id1 = form["id"];
string name1 = form["name"];
return null;
}
小結:
其實這四種傳遞方式很相似,用哪個都可以,吧界面上的值設定好了,name屬性的使用,form action,以及type屬性的使用,這幾個寫好了,其他的都一樣了。
現在覺得學習總結這些知識真的很有意思,I have fallen in love with it.