【问题标题】:Why Ajax.BeginForm redirect to new empty page after submission?为什么 Ajax.BeginForm 提交后重定向到新的空白页面?
【发布时间】:2014-03-14 07:03:22
【问题描述】:

为什么 Ajax.BeginForm 在提交后将我的页面重定向到新的空白页面?

我的控制器代码是:

    [HttpPost]
    public void ProductCommentAdd(int productId, string text)
    {
           //Do something
    }

视图中的 Mvc ajax 调用是:

    @using (Ajax.BeginForm("ProductCommentAdd", "Shop", new AjaxOptions() { HttpMethod = "POST"}))
    {
                <input type="hidden" value="@Model.ProductId" name="ProductId"/>
                <textarea name="text"></textarea>
                <input type="submit" value="Submit"
    }

当我点击提交按钮时,我的页面重定向到新的空白页面。我该如何解决?

【问题讨论】:

  • 你有“ProductCommentAdd”的愿景吗?
  • 您确定页面中包含unobtrusive.ajax.js 文件吗?

标签: c# asp.net-mvc asp.net-ajax


【解决方案1】:

您需要在页面中包含以下脚本:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" 
                       type="text/javascript"></script>

【讨论】:

    【解决方案2】:

    正如@Mat J 和@user3206982 所​​建议的那样:您可能需要 unobtrusive.ajax.js 文件。

    您可以从 nuget 下载该软件包,然后 将其添加到捆绑配置中:

    bundles.Add(new ScriptBundle("~/bundles/jqueryunobtrusive").Include(
            "~/Scripts/jquery.unobtrusive*"));
    

    在 html 中:

    @Scripts.Render("~/bundles/jqueryunobtrusive")
    

    来源:https://dotnetthoughts.net/mvc5-ajax-form-is-not-updating-div-but-replacing-the-whole-page-instead/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多