jQuery MVC 3 Razor - Ajax.BeginForm OnSuccess

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4927479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:11:35  来源:igfitidea点击:

MVC 3 Razor - Ajax.BeginForm OnSuccess

jqueryasp.net-mvcajaxasp.net-mvc-3

提问by user547043

I'm new to MVC and I'm trying to update my page after I submit my form; but it's not working. I'm only trying to hide the form, and show the contents of a div OnSuccess.

我是 MVC 的新手,我正在尝试在提交表单后更新我的页面;但它不起作用。我只是想隐藏表单,并显示 div OnSuccess 的内容。

My code is:

我的代码是:

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

    function MessageConfirmation() {
        $('#confirmation').show('slow');
        $('#contactForm').hide('slow');
    }

</script>

@using (Ajax.BeginForm("Index", new AjaxOptions { OnSuccess = "MessageConfirmation" }))
{
<fieldset id="contactForm">
    <legend>Message</legend>
    <p>
        @Html.Label("Email", "Email"): @Html.TextBox("Email")
    </p>
    <p>
        @Html.Label("Subject", "Subject"): @Html.TextBox("Subject")
    </p>
    <p>
        @Html.Label("Message", "Message"): @Html.TextArea("Message")
    </p>
    <p>
        <input type="submit" value="Send" />
    </p>
</fieldset>

<p id="confirmation" onclick="MessageConfirmation()">
    Thanks!!!
</p>
}

Any alternate solutions / ideas are most welcome.

任何替代解决方案/想法都是最受欢迎的。

Thanks in advance!

提前致谢!

回答by Darin Dimitrov

Not workingis a problem description that's more adapted to people that don't know/care about how computer works and not software developers. Software developers usually describe precisely the problem they are having. They post the exact error message/exception stack trace they are having.

不工作是一个问题描述,更适合不知道/关心计算机如何工作的人而不是软件开发人员。软件开发人员通常会准确地描述他们遇到的问题。他们发布了他们所拥有的确切错误消息/异常堆栈跟踪。

This being said you are asking for alternative solutions, here's mine: don't use any MS Ajax.* helpers, use jquery directly and unobtrusively, like this

话虽如此,您正在寻求替代解决方案,这是我的:不要使用 any MS Ajax.* helpers,直接和不显眼地使用 jquery,就像这样

<script type="text/javascript">
    $(function () {
        $('#confirmation').hide();
        $('form').submit(function() {
            $('#confirmation').show('slow');
            $(this).hide('slow');
            return false;
        });
    });
</script>

@using (Html.BeginForm())
{
    <fieldset id="contactForm">
        <legend>Message</legend>
        <p>
            @Html.Label("Email", "Email"): 
            @Html.TextBox("Email")
        </p>
        <p>
            @Html.Label("Subject", "Subject"): 
            @Html.TextBox("Subject")
        </p>
        <p>
            @Html.Label("Message", "Message"): 
            @Html.TextArea("Message")
        </p>
        <p>
            <input type="submit" value="Send" />
        </p>
    </fieldset>
}

<p id="confirmation">
    Thanks!!!
</p>

Notice how the confirmationparagraph has been externalized from the form.

注意confirmation段落是如何从表单中具体化的。

回答by Artiom Chilaru

Are you sure you have all the correct js files referenced in your project? In MVC3, they have moved away from the MS Ajax files. If I remember right - the unobtrusive javascript should be enabled by default, so you should reference the following files: jquery.js, jquery.validate.js, jquery.valudate.unobtrusive.js, jquery.unobtrusive-ajax.js

您确定您的项目中引用了所有正确的 js 文件吗?在 MVC3 中,它们已经远离 MS Ajax 文件。如果我没记错的话——默认情况下应该启用不显眼的 javascript,所以你应该参考以下文件:jquery.js、jquery.validate.js、jquery.valudate.unobtrusive.js、jquery.unobtrusive-ajax.js

With these files references - you should be fine.

有了这些文件参考 - 你应该没问题。

P.S. There's a very good blog post by Brad Wilson explaining the details of unobtrusive ajax in MVC3, and how it all works. Check it out here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-ajax.html

PS Brad Wilson 有一篇很好的博客文章,解释了 MVC3 中不显眼的 ajax 的细节,以及它是如何工作的。在这里查看:http: //bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-ajax.html

回答by Brian Mains

Make sure you can get to your action method. Also add a failure handler and see if its returning a failure too.

确保您可以使用您的操作方法。还要添加一个失败处理程序,看看它是否也返回失败。

The unobtrusive features must be enabled too, and you must add in the MVC unobtrusive AJAX scripts for MVC 3 and razor.

也必须启用不显眼的功能,并且您必须为 MVC 3 和 razor 添加 MVC 不显眼的 AJAX 脚本。

HTH.

哈。

回答by Stian

You could use JQuery trigger

你可以使用 JQuery 触发器

$("#confirmation").trigger('click');

Because you have a onclick method on the div

因为你在 div 上有一个 onclick 方法