Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 964
Hadi Sharifi Avatar asked Feb 14 '14 19:02

Hadi Sharifi


2 Answers

you need to include the following script to your page:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" 
                       type="text/javascript"></script>
like image 53
user3206982 Avatar answered Nov 06 '22 10:11

user3206982


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/

like image 3
hormberg Avatar answered Nov 06 '22 10:11

hormberg