Ajax.BeginForm replaces the entire page with a partial view

I'm having problems using Ajax.BeginForm()ASP.NET MVC in my simple application. I was looking for SO for a solution, but didn't find anything, it seems to help.

I created a new project using the default Visual Studio 2015 template. I changed the file Index.cshtmlto:

@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div id="ajax-test"></div>

    @using (Ajax.BeginForm("Test", new AjaxOptions()
    {
        UpdateTargetId = "ajax-test",
        InsertionMode = InsertionMode.Replace
    }))
    {
        <input type="submit" />
    }
</div>

I added a new action to HomeController:

    public ActionResult Test()
    {
        return PartialView("Test");
    }

New view Home\Test.cshtml:

Hello world

And, importantly, I added a link to the necessary JavaScript files on _Layout.cshtml(moving them to the top of the page does not help).

    ...
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

I checked that the package jqueryvaldisplays in debug mode as:

<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

These entries were already present in the file web.config:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

, "", div "Hello world". "Hello world", /Home/Test. .

? , StackOverflow , , JavaScript, . , , , .

+4
1

5 StackOverflow. .

NuGet Microsoft.jQuery.Unobrusive.Ajax, , jquery.unobtrusive-ajax.js . , jquery.validate.unobtrusive.js -, .

+7

Source: https://habr.com/ru/post/1609195/


All Articles