title: Post form data to Controller's action with Ajax
body: <p>I have a page in MVC3, with a link (Ajax.ActionLink). When user clicks it, it calls controller's action, and the result is inserted into a div, with replace.</p>
  
  <p>Code is shown below:</p>
  
  <pre><code>@Ajax.ImageActionLink("/Images/btn_share.png", "Share pool", "SharePool", new { poolKey = Model.Id, poolName = Model.Name },
      new AjaxOptions { 
          UpdateTargetId="popup", 
          HttpMethod="GET", 
          InsertionMode = InsertionMode.Replace,
          LoadingElementId="loading_dialog",
          OnSuccess = "ShowPopup('#popup_share', true, true)"
      } 
  </code></pre>
  
  <p>ImageLinkAction is custom extension method to use image as link, and ShowPopup is a javascript function that shows the updated div (to make it look as a popup)</p>
  
  <p>Now the markup code inserted into the div which creates the popup contains a form as below</p>
  
  <pre><code>&lt;div&gt;
  @using (Html.BeginForm()) {
  
      @Html.HiddenFor(model =&gt; model.ID)
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.EmailAddress)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.EmailAddress)
          @Html.ValidationMessageFor(model =&gt; model.EmailAddress)
      &lt;/div&gt;
  
      // ... other fields
  
      @Html.ValidationSummary(true)
      &lt;p&gt;
          &lt;button type ="submit"&gt;Share&lt;/button&gt;       
      &lt;/p&gt;
  }
  &lt;/div&gt;
  </code></pre>
  
  <p>The issue is with the form's submit: the Submit button calls the proper action but with a postback, which cause my page to refresh. What I need is to post data with ajax, receive the response, which is another partial view that gets inserted into the </p>
  
  <p>I was trying to replace the Submit button with Ajax.ActionLink as below</p>
  
  <pre><code>    @Ajax.ActionLink("Share", "Share",
          new Models.MyModel 
              {
                  ID = Model.ID,
                  EmailAddress = Model.EmailAddress
              },
          new AjaxOptions
              {
                  UpdateTargetId="popup", 
                  HttpMethod="POST", 
                  InsertionMode = InsertionMode.Replace,
                  LoadingElementId="loading_dialog",
                  OnSuccess = "ShowPopup('#popup_share', true, true)"
              }
  </code></pre>
  
  <p>The controller's code looks like this:</p>
  
  <pre><code>[HttpPost]
  public ActionResult SharePool(MyModel model)
  {
      // ...
      return PartialView("_MyPartialView", model)
  }
  </code></pre>
  
  <p>The problem is, in the moment the Ajax ActionLink is rendered (when form is loaded) there is no value in Model.EmailAddress, so my POST action in controller receives only ID parameter.</p>
  
  <p>How can I handle this? Ideally, I think I should add                     </p>
  
  <pre><code>OnBegin = "PreparePostData()"
  </code></pre>
  
  <p>But since I know javascript only basically, I have no idea how can I implement this. I think this PreparePostData() should collect form fields and prepare the object routeValues parameter, to be set before the ajax call is invoked.</p>
  
  <p>Anyone can give me some indications on how to implement this?</p>
  
  <p>Or is any other, better approach on this problem?</p>
  
  <p>Thank you</p>