您的位置:首页 - 教程 - ASP.NET - 正文
jQuery post调用asp.net mvc控制器参数为FormCollection
控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace jQueryFormCollection.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CheckSelection(FormCollection collection)
{
//return View();
return Content("form submitted");
}

public ActionResult MyView(FormCollection collection)
{
return View();
//return Content("form submitted");
}

[AcceptVerbs(HttpVerbs.Post)]//何问起
public ActionResult PostFormCollection(FormCollection collection) {
return Content(collection.Count.ToString());
}
}
}


html文档代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Post 到ASP.NET MVC 控制器</title>
</head>
<body>
<form action="/Check/PostFormCollection" id="CheckForm" method="post">
<fieldset>
<div class="editor-label">
<input checked="checked" id="CodeExist" name="CodeExist" type="checkbox" value="true" /><input name="CodeExist" type="hidden" value="false" />
<label for="Check_Code_Existence">Check Code Existence</label>
</div>
<div class="editor-label">
<input checked="checked" id="Mandatory" name="Mandatory" type="checkbox" value="true" /><input name="Mandatory" type="hidden" value="false" />
<label for="Check_Code_Reccurence">Check Code Reccurence</label>
</div>
<div class="editor-label">
<input checked="checked" id="Reccurence" name="Reccurence" type="checkbox" value="true" /><input name="Reccurence" type="hidden" value="false" />
<label for="Check_Mandatory_Code">Check Mandatory Code</label>
</div>
</fieldset>
</form>
<script src="http://down.hovertree.com/jquery/jquery-1.12.4.js"></script>
<script>
var form = $("#CheckForm");
var formCollection = form.serialize();
//alert(formCollection);
console.log(formCollection);
$.post('/Home/PostFormCollection', formCollection , function (data) {
alert("何问起提示:提交参数个数为"+data); //by 何问起
});
</script>
</body>
</html>

效果如图:

console.log(formCollection);
输出内容为:
CodeExist=true&CodeExist=false&Mandatory=true&Mandatory=false&Reccurence=true&Reccurence=false

评论: