1

I have an ajax form:

@model Site.Models.ChangeModel
@using (Ajax.BeginForm("ChangePassword", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId="result" }, new { @class = "form-horizontal", role = "form", id = "changePasswordForm" }))
{
    <div id="result"></div>
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, Base.ChangesNotSubmitted, new { @class = "alert alert-danger" })

    @Html.EditorFor(m => m.ChangePasswordModel.OldPassword)

    @Html.EditorFor(m => m.ChangePasswordModel.Password)

    @Html.EditorFor(m => m.ChangePasswordModel.ConfirmPassword)

    <div class="form-group">
        <div class="col-sm-offset-6 col-sm-2">
            <button type="submit" class="btn btn-primary">@Base.ChangePassword</button>
        </div>
    </div>
}

With this action

   [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ChangePassword(ChangePasswordModel model)
    {
        bool changed=false;
        if (ModelState.IsValid)
        {
            var result = UserManager.ChangePassword(User.Identity.GetUserId(), model.OldPassword, model.Password);
            if (result.Succeeded)
            {
                changed = true;
            }
        }

        return Content("changed");

    }

The form is located in the action Index, and i don't have view for ChangePassword!

After submitting the sform I get:

  • A blank page with content i've sent
  • Url is changed to .../ChangePassword?length=7 (before it was /index)

I have no idea where this length parameter comes from.

So my question is how to stay on the same view and to get the result in the correct div?

And anyone any idea where the parameter length comes from, i already has this issue few times with it.

1
  • Are you referencing all the necessary scripts? The most common problems I've seen with Ajax.BeginForm() is not referencing all scripts or referencing them in the wrong order. Commented Feb 16, 2014 at 9:54

3 Answers 3

2

In case someone comes across this regarding the length parameter (your second question above), you can change the method call as follows:

@using (Ajax.BeginForm("ChangePassword", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId="result" }, new { @class = "form-horizontal", role = "form", id = "changePasswordForm" }))

//to:

@using (Ajax.BeginForm("ChangePassword", "Account", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId="result" }, new { @class = "form-horizontal", role = "form", id = "changePasswordForm" }))

The length parameter of /ChangePassword?length=7 will go away. The length comes from the number of characters in your controller name "Account".

Sign up to request clarification or add additional context in comments.

Comments

1

Please be sure that you have included the ajax javascript files, in the header.

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

Comments

0

You have to use some like this:

return RedirectToAction("ViewName", [new { args } OR model]);

The ContentResult may be used to return to an action as plain text. This class is inherited from the "ActionResult" abstract class.

13 Comments

but ajax suppose to stay on the same page, why it renders me another page it is strange :(
For exampe i have a div expanded but after submit it is closed :/ is this a correct behaviour?
Yes, because your ajax replacing form: pdateTargetId = "result"
but this div is inside form
and when you returning Content("") you'll get a new page with this content, named by ActionResult
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.