14

I'm trying to convert a form from synchronous to asynchronous using the Ajax.BeginForm helper method in MVC 4.

In my view I have:

   @{
        ViewBag.Title = "Users > Edit";
        var options = new AjaxOptions()
                          {
                              Url = Url.Action("Edit", "User"),
                              LoadingElementId = "saving",
                              LoadingElementDuration = 2000,
                              Confirm = "Are you sure you want to save this User?"
                          };  
    }

    <div id="saving" style="display:none; color:Red; font-weight: bold"> 
         <p>Saving...</p> 
    </div> 

    @using (Ajax.BeginForm(options)) 
    {
        @Html.ValidationSummary(true)
        <fieldset>
            .... FIELDS ...
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

When the submit button is clicked a complete postback is being executed. My action method looks like this:

[HttpPost]
public ActionResult Edit(User user)
{
    if (ModelState.IsValid)
    {
        _repository.Save(user);
        TempData["message"] = String.Format("{0} has been saved.", user.Username);
    }

    return View(user);
 }

Is there something I'm missing? Has the MVC 4 current release got a few problems?

In my Web.Config I do have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled set to true.

I also have these specified in my _Layout.cshtml:

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
1
  • 2
    Important note; jquery-1.9.0.min.js not support Ajax.FormBegin and Ajax.ActionLink :( Commented Jan 20, 2013 at 20:48

3 Answers 3

28

Is there something I'm missing?

Maybe you are missing the unobtrusive ajax script:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

Yep ha you spotted it! Had validate.unobtrusive.js etc etc. Doh! Thanks :D
0

if i am understood correct, you are trying to post the data to controller, after successful you need to go to home page ?? if not can you clarify. so that will try to help you

if (ModelState.IsValid)
    {
        _repository.Save(user);
        TempData["message"] = String.Format("{0} has been saved.", user.Username);
        return RedirectToAction("yourActionToRedirect");  // after succesfull it will go to the index view
    }

3 Comments

In this example I'd actually like to return to the current view with the same user data e.g. return View(user);
then what is the problem?? did try to debug it ?? is it hitting the action on User Controller ??
That it's not behaving in an asynchronous manner. The page is being posted back and Request.IsAjaxRequest() returns false in the Edit action.
0

I had the same kind of problem... I've updated all the js files with nuget package console.

just my 2 cents, but also at the time of writing, the jquery.unobtrusive.ajax file does contain calls to the deprecated jquery function '.live' (=> it's deleted from jquery 1.9+ version) jquery live

I think there are like 4 occurrences that you'll have to change with .on, now it works fine ;-)

Comments

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.