本文實(shí)例講述了ASP.NET在MVC控制器中獲取Form表單值的方法。分享給大家供大家參考,具體如下:
在MVC控制器中,如果我們想直接獲取表單中某個(gè)標(biāo)簽元素的值,可以使用MVC中提供的FormCollection類,具體用法如下所示:
視圖部分:
1
2
3
4
5
6
|
@ using (Html.BeginForm()) { <text>您輸入的值是:</text><span>@ViewBag.FormValue</span> <input type= "text" name= "txtName" id= "txtName" value= "" /> <input type= "submit" name= "btnSave" id= "btnSave" value= "提 交" /> } |
控制器部分:
1
2
3
4
5
6
|
[HttpPost] public ActionResult Index(FormCollection formCol) { ViewBag.FormValue = formCol[ "txtName" ]; return View(); } |
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。