user971741
user971741

Reputation:

Ajax.BeginForm not doing an asynchronous post

I am having problem with Ajax.BeginForm for long now is search over the internet and SO but most of solutions of including jquery.unobtrusive-ajax.min or editing web.config to allow UnobtrusiveJavaScriptEnabled which in my case both are done but still my ajax form is doing a full post back and can’t understand why.

In View

@using (Ajax.BeginForm("Login", "User", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "loginResult" }, new { @id = "loginForm", @name = "loginForm" }))
    {
        <span id="loginResultx">@(TempData["loginResult"] != null ? TempData["loginResult"].ToString() : "")</span>
        <span id="loginResult"></span>
        <span>
            @Html.Label("User Id")
            @Html.TextBox("id")</span>
        <span>@Html.Label("Password")
            @Html.Password("pwd")
        </span>
        <span>
            <input type="submit" value="Submit" />
        </span>
    }

in Controller

public ActionResult Login(FormCollection loginForm)
    {
        if (Request.HttpMethod == "POST")
        {
            var id= loginForm["id"];
            var pwd = loginForm["pwd"];
            bool status = false;
            User obj = new User();
            status = obj.Login(id, pwd);
            if (status)
            {
                Session["id "] = id;
                return RedirectToRoute("Profile");

            }

            TempData["loginResult"] = "Login Failed";
        }
        return View();
    }

In BundleConfig

bundles.Add(new ScriptBundle("~/scr").Include("~/Scripts/jquery-1.7.1.min.js", "~/Scripts/modernizr-2.5.3.js", "~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.unobtrusive-ajax.min"));

Upvotes: 2

Views: 2039

Answers (1)

Moby&#39;s Stunt Double
Moby&#39;s Stunt Double

Reputation: 2550

Are you sure it's not this line that is the problem?

bundles.Add(new ScriptBundle("~/scr").Include("~/Scripts/jquery-1.7.1.min.js", "~/Scripts/modernizr-2.5.3.js", "~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.unobtrusive-ajax.min"));

You are missing a .js from the ajax file include. Do you see it?

Either way, I would use Chrome inspector/Fiddler to analyse any JS errors or missing includes.

Upvotes: 2

Related Questions