- 在網址後加上參數名稱和參數值
- ?name1=value1&name2=value2&...
Form表單
- 透過form及submit將form裡的參數送至controller,處理少量參數
- 範例:
Action:
public ActionResult ModelBind(string name)
{
ViewBag.Name=name;
return View();
}
View :
<form action="/cont/ModelBind" method="post">
Name:<input type="text" name="name"/>
<input type="submit" value="Submit" />
</form>
<p>Your Name : @ViewBag.Name</p>
- FormCollection類別,處理傳遞多個參數
- 範例:
Action:
public ActionResult ModelBind(FormCollection form)
{
ViewBag.Name=form["name"];
ViewBag.Age=form["age"];
return View();
}
View :
<div>
@using(Html.BeginForm()){
Name:<input type="text" name="name"/><br/>
Age: <input type="text" name="age"/><br/>
<input type="submit" value="Submit" />
}
</div>
<p>
Your Name : @ViewBag.Name<br/>
Your Name : @ViewBag.Age
</p>
RouteData
- 透過路由範本取得參數值
- /{Controller}/{Action}/{id}
JSON
- 範例:
Model:
public class Person
{
public string id {get;set;}
public string name {get;set;}
public string email {get;set;}
}
Action:
[HttpPost]
public ActionResult ExJson(Person[] p)
{
return Json(p, JsonRequestBehavior,AllowGet);
}
沒有留言:
張貼留言