html 表单控制器,c# – html表单发布到mvc控制器
將Html發(fā)布到MVC控制器
>使用表單創(chuàng)建HTML頁面(不要忘記引用Jquery.js)
User Name
Password
$(document).ready(function () {
//get button by ID
$('#btn1').submit(function () {
//call a function with parameters
$.ajax({
url: 'rec/recieveData', //(rec)= Controller's-name
//(recieveData) = Action's method name
type: 'POST',
timeout: '12000', (optional 12 seconds)
datatype: 'text',
data: {
//Get the input from Document Object Model
//by their ID
username: myform.username.value,
password: myform.password.value,
}
});
});
});
然后在MVC控制器中
controller/action
| |
1.創(chuàng)建名為rec的rec(rec / recieveData)
>創(chuàng)建名為rec.cshtml的視圖
這是控制器:
public class recController : Controller
{
// GET: rec
string firstname = "";
string lastname = "";
List myList = new List();
public ActionResult recieveData(FormCollection fc)
{
//Recieve a posted form's values from parameter fc
firstname = fc[0].ToString(); //user
lastname = fc[1].ToString(); //pass
//optional: add these values to List
myList.Add(firstname);
myList.Add(lastname);
//Importan:
//These 2 values will be return with the below view
//using ViewData[""]object...
ViewData["Username"] = myList[0];
ViewData["Password"] = myList[1];
//let's Invoke view named rec.cshtml
// Optionaly we will pass myList to the view
// as object-model parameter, it will still work without it thought
return View("rec",myList);
}
}
這是視圖:
@{
ViewBag.Title = "rec";
}
Hello from server
@ViewData["Username"]
@ViewData["Password"]
總結(jié)
以上是生活随笔為你收集整理的html 表单控制器,c# – html表单发布到mvc控制器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: left join、right join
- 下一篇: not support mysql_MY