Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 AJAX Submit Form not working

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>
like image 445
adamwtiko Avatar asked Jan 30 '12 15:01

adamwtiko


1 Answers

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>
like image 133
Darin Dimitrov Avatar answered Sep 28 '22 06:09

Darin Dimitrov