Why Ajax.BeginForm redirect to new empty page after submission?

北慕城南 提交于 2019-12-01 16:14:05

问题


Why Ajax.BeginForm redirect my page to new empty page after submission?

My controller code is:

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

Mvc ajax call in view is:

    @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"
    }

When I click submit button my page redirect to new empty page. How I can fix it?


回答1:


you need to include the following script to your page:

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



回答2:


As @Mat J and @user3206982 suggests: you probably need the unobtrusive.ajax.js file.

You can download the package from nuget and add it in the bundles config:

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

and in the html:

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

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



来源:https://stackoverflow.com/questions/21787489/why-ajax-beginform-redirect-to-new-empty-page-after-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!