title: ASP MVC form to call specific action and not redirect but update original page
body: <p>I am using ASP.net MVC 3 for a project, and at a point now where I understand more of it, but also trying to learn differences between it and the WebForms world I have used for so long. Previously in WebForms you would just put the fields out there, add a button, and create an event, and then handle whatever you need to with with the postback.</p>
  
  <p>For example, I have a simple view that shows details about an error. I want to push some of the information about this error to our issue tracker's API. Lets say just the subject and content of the error. I would in HTML do something like:</p>
  
  <pre><code>&lt;form action="@Url.Action("Send")" method="post"&gt;
      &lt;input id="subject" name="subject" type="text" value="@Model.subject" /&gt;
      &lt;br /&gt;
      &lt;input id="content" name="content" type="text" value="@Model.content" /&gt;
      &lt;br /&gt;
      &lt;input type="submit" value="Send" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I found that naming my input ID's to match parameter names in my controller would then let me pass these parameters. This is similar to what you see in this older post by <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" rel="nofollow">Scott Guthrie</a>.</p>
  
  <p>First of all, I want to call the 'Send' action in my Errors controller, and not my Details one. This actually does call my 'Send' action, <strong>but then redirects to localhost/Errors/Send, which I don't want it to.</strong> </p>
  
  <p>I want to be able to submit this form, which calls and action and does the work with the remote API, and then update a message on the current page stating that the error has been transferred. </p>
  
  <p>Once the issue had been submitted to the bug tracker, how do I then, for example, show a DIV on the original page passing content back to it (for example, a link to the issue in the issue tracker)?</p>
  
  <p><strong>Update:</strong> Here is what I am doing in my controller:</p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/redmine/issues.json");
  request.Credentials = new NetworkCredential("username", "password");
  request.Method = "POST";
  request.ContentType = "application/json";
  request.Accept = "application/json";
  
  using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  {
      writer.Write("'issue': { " +
                        "'project_id': 'project', " +
                        "'subject': 'Test issue'" +
                        "}");
  }
  
  WebResponse response = request.GetResponse();
  Stream stream = response.GetResponseStream();
  string json = "";
  
  using (StreamReader reader = new StreamReader(stream))
  {
      json = reader.ReadToEnd();
  }
  </code></pre>
  
  <p>Also, here is what I am doing in my view (simplified, and after help from Phillip)</p>
  
  <pre><code>@model webapp.Models.app_errors
  
  &lt;script type="text/javascript"&gt;
   function SendErrors() {    
  
             alert(@Model); // here I get a 'webapp undefined error'
             alert(Model); // Model is undefined. If I reference @Model.App_name or another value anywhere, works fine in rest of view
  
          $.ajax({
              type: 'POST', //(probably)
              url: '../SendToRedmine',
              cache: false, //(probably)
              dataType: 'json', //(probably)
              data: @Model,
              success: function(result)
              {
                  alert("The error has been transferred");
                  //you can also update your divs here with the `result` object, which contains whatever your method returns
              }
              });
      }
  &lt;/script&gt;
  
  &lt;input type="button" onclick='SendErrors()'/&gt;
  </code></pre>