JQuery在ASP.NET MVC 3.0 FormCollection中发布表单 [英] JQuery Post Form in ASP.NET MVC 3.0 FormCollection

查看:185
本文介绍了JQuery在ASP.NET MVC 3.0 FormCollection中发布表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Jquery中将表单提交给Controller中的Action方法。
为此,我序列化表单并使用get方法。在控制器中,我收到我的表单,像param1 = 1& param2 = 2 ....
这样的字符串有没有一种方法可以直接在我的Action Method而不是字符串中检索FormCollection。由于我的表单有很多复选框,所以我可以更容易地将它放在formCollection中。

I try to submit a form in Jquery to an Action method in a Controller. To do so I serialize my form and use the get method. In the controller I receive my form as a string like param1=1&param2=2.... Is there a way to retrieve directly a FormCollection in my Action Method instead of the string. As my form has many checkboxes it will be easier for me to have it in formCollection.

这是我的jquery:

Here is my Jquery:

 var form = $("#CheckForm");
                    var formCollection = form.serialize();
                    $.post('@Url.Action("CheckSelection")', { clientId: clientId, policyId: policyId, countryCode: country, month: monthtoken[0] + '*' + monthtoken[1], formCollection: formCollection }, function () {
                        alert("formSubmit");
                    });

这里是我的表单:

Here my form:

@using (Html.BeginForm("CheckSelection", "Check", FormMethod.Post, new { id = "CheckForm" }))
{        
<fieldset>
<div class="editor-label">
    @Html.CheckBox("CodeExist",true)
    @Html.Label("Check Code Existence")
</div>
<div class="editor-label">
    @Html.CheckBox("Mandatory",true)
    @Html.Label("Check Code Reccurence")
</div>
<div class="editor-label">
    @Html.CheckBox("Reccurence",true)
    @Html.Label("Check Mandatory Code")
</div>
}

这是我的操作方法:

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CheckSelection(string clientId, string policyId, string countryCode, string month, FormCollection formCollection){}

预先感谢您的帮助!

Thanks in advance for your help!

推荐答案

var form = $("#CheckForm");
var formCollection = form.serialize();
$.post('@Url.Action("CheckSelection")', formCollection , function (data) {
    alert(data); //will alert form submitted
});

并且控制器看起来像

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

获取FormCollection类的详细信息,请参阅 link

for details of the FormCollection class follow this link

这篇关于JQuery在ASP.NET MVC 3.0 FormCollection中发布表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆