简体   繁体   English

使用Jquery表单插件asp.net mvc 3

[英]Using Jquery form plugin asp.net mvc 3

I have this code in the View: 我在视图中有这个代码:

<script src="../../Scripts/jquery.form.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {
    $('#myForm').ajaxSubmit();
});

@using (Html.BeginForm("PostData","Home",FormMethod.Post)) {

 <input type="text" name="name" />
 <input type="submit" value="submit" />
}

Then in the controller I have this method: 然后在控制器中我有这个方法:

 [HttpPost]
    public void PostData(string name)
    {
        //do smoething with name
    }

The problem is that I get redirected the url /home/PostData when submitting the form. 问题是我在提交表单时重定向了url / home / PostData。

Anyone got any suggestions? 有人有什么建议吗?

ajaxSubmit() submits the form immediately. ajaxSubmit()立即提交表单。 You need ajaxForm() in order to AJAXify an existing form: 您需要ajaxForm()才能AJAX化现有表单:

$(document).ready(function () {
    $('#myForm').ajaxForm();
});

once a form has been AJAXified you could later force its submission with $('#myForm').ajaxSubmit(); 一旦表单被AJAX化,你可以稍后用$('#myForm').ajaxSubmit();强制提交.ajaxSubmit $('#myForm').ajaxSubmit(); or simply leave it to the user to press the submit button. 或者只是将其留给用户按下提交按钮。

Also the way you have defined your form it doesn't seem to have an id. 另外,您定义表单的方式似乎没有id。 So your $('#myForm') selector is unlikely to return anything. 所以你的$('#myForm')选择器不太可能返回任何东西。 You might want to assign an id to your form: 您可能希望为表单指定一个ID:

@using (Html.BeginForm("PostData", "Home", FormMethod.Post, new { id = "myForm" })) 
{
    <input type="text" name="name" />
    <input type="submit" value="submit" />
}

or if you don't want to assign unique id to the form you could AJAXify all forms on the current view by adapting your jQuery selector: 或者如果您不想为表单指定唯一ID,则可以通过调整jQuery选择器来AJAX化当前视图上的所有表单:

$(document).ready(function () {
    $('form').ajaxForm();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM