JQuery AJAX Post Inside Partial View

本文关键字:Partial View Inside Post AJAX JQuery | 更新日期: 2023-09-27 18:10:48

在我的c# MVC4应用程序中,我有两个主要视图:"Index"answers"Index_Perm "。登录用户的角色决定呈现哪些视图。"Index"包含一个名为"PMain"的视图,"Index_Perm"包含一个名为"PMain_Lim"的视图。"PMain"answers"PMain_Lim"包含另一个称为"Analysis"的部分视图。在"Analysis"里面,我有这个脚本:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('#SubmitAverage').click(function () {
            var $form = $('#form1');
            $.ajax({
                type: "POST",
                url: "Home/Average",
                data: $form.serialize(),
                success: function () {
                    alert("Edit successful");
                },
                error: function () {
                    alert('failure');
                }
            });
        });
    });
    </script>

我的问题是,当"索引"是被访问的主视图,这个脚本正确运行时,提交按钮被点击。当"Index_Perm"是主视图,然而,当按钮被点击,AJAX后失败的错误:The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. The directory or file specified does not exist on the Web server.经过一些调查在Firebug,我看到它试图访问:mylocalhost/Home/Index_Perm/Home/Average而不是:mylocalhost/Home/Average指定为url发送post在我的脚本。这里是代码的适用部分,这是相同的"PMain"answers"PMain_Lim",其中包含绑定到脚本的按钮:

    <div id="Analysis" title="Analysis">
        @Html.Action("Analysis", "Home", Model)
    </div>
</section>
<section>
         <h3>
            <button class="btn" id="SubmitAverage" value ="SubmitAverage" name="action:SubmitAverage" type="button">Submit Averages</button>
        </h3>
    </section>
<section>
    <div id="OtherView" title="OtherView" class="OtherChanges">
        @Html.Action("OtherView", "Home", (string)ViewBag.SearchKey)
    </div>
</section>

有没有人知道为什么它忽略了我指定的URL在JQuery AJAX POST和/或如何纠正它?

JQuery AJAX Post Inside Partial View

在ajax post中使用urlhelper

url: '@Url.Action("Average","Home")',