3

I have a View made up of several partial views, one of which displays user information with an Edit button.

When the Edit button is clicked, I make an ajax call to another Action that returns a Partial View which is loaded into a JQuery-UI modal dialog.

How do I submit this edit form via Ajax and update the UserInfo partial view on the main page?

2
  • look here mrgsp.md:8080/awesome/foobar (click create or edit) Commented Jan 27, 2011 at 15:04
  • Thats pretty much what I need to do..but that looks like its just a demo...not a tutorial or anything... Commented Jan 27, 2011 at 15:34

2 Answers 2

6

Ajax Call

$('#submitIt').submit(function() {
    var createdBy = $('#createdBy').val();
    $.ajax({
         type: "POST",
         url: '/MyController/GetContact/',
         dataType: "html",
         data: { 'createdBy': createdBy },
         success: function (result) {
            $('#myLittleForm').html(result);
         },
         error: function (request, status, error) {
              //Do Something on Failure
         }
     });
});

Controller

    [HttpPost]
    public ActionResult GetContact(string createdBy)
    {
         ViewData["CreatedBy"] = createdBy;
         return PartialView("MyView");
    }

Markup

<div id="myLittleForm">
   <form action="/MyController/GetContact/" method="post">
      <input id="createdBy" type="text"/> <br/>
      <input id="submitIt" type="submit" value="Submit"/>
   </form>
</div>

Note

When you submit the form under markup, the ajax call is made and the div "myLittleForm" is replaced with your partial view.

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

2 Comments

Making the ajax call is not my problem... basically I need to know how the controllers will handle the ajax post then update the partial view on the main page(without having to reload the entire main page)...
Ok I think i got it now. I will give this a try.
2

Edit: oops, the scripts didn't show - fixing You'll need to reference the Ajax scripts shown here:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

<script src="../../Scripts/AjaxLoadedContentScriptFix.js" type="text/javascript"></script>

I don't recall what that last script is, but I'm using it in my app so it probably can't hurt :)

Then you'll need to use Ajax.BeginForm in your view to post a form asynchronously. It takes an AjaxOptions parameter that will allow you to specify the update target, success callbacks, etc. Example:

<% Ajax.BeginForm("FormName" , new { id = Model.SomeProperty }, 
       new AjaxOptions { UpdateTargetId = "MyDivToUpdate", OnSuccess = "onSuccess" }); %>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.