title: How do I return two different Views from the same Action in ASP.NET MVC?
body: <p>I have two views which will both use the same Controller method:</p>
  
  <pre><code>//webServiceController.cs
  
  //The actual method is about 40 lines of code. Truncated for readability.
  public ActionResult Index()
  {
      object i = new List&lt;WebServiceMethod&gt;();
      i = svcService.populateList("Programs");
  
      return View(i);
  
  }
  </code></pre>
  
  <p>The first view is an HTML page that displays the data in a pretty table output:</p>
  
  <pre><code>&lt;% // Index.aspx %&gt;
  
  &lt;table&gt;
  &lt;tbody&gt;
  &lt;% foreach (var item in Model) { %&gt;
  
  &lt;tr&gt;
      &lt;td&gt;
          &lt;% if (Convert.ToInt32(item.numberRequests) &gt; 0)
          {%&gt;
              &lt;%= Html.ActionLink("Details", "Details", new { programNumber = item.programNumber })%&gt;
          &lt;%} %&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;%= Html.Encode(item.programNumber) %&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;% } %&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>The second view is a quick'n'dirty conversion to JSON so that I can do magical AJAX tricks with the data:</p>
  
  <pre><code>&lt;% 
      // AjaxGetServiceData.aspx
  
      // Convert web service response object into JSON for AJAX.
      var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
      Response.Write(jss.Serialize(Model));
  
  %&gt;
  </code></pre>
  
  <p>I'd created a duplicate of the <code>Index()</code> method and called it <code>AjaxGetServiceData()</code>, but that defeats the purpose of MVC.</p>
  
  <hr>
  
  <p><strong>Resolution:</strong></p>
  
  <p>I didn't ask my question very well, as evidenced by a 5-10 minute discussion I just had with a coworker about this very topic. He kept asking me the same question that many users on this page asked me: "How does the controller know which view to return?" I responded, "That's what I'm trying to figure out." I was trying to get the method to return a different view (or Json output) when AJAX was the requester. A string argument in the method was my solution.</p>
  
  <p>This is what I ended up using to get my desired effect:</p>
  
  <pre><code>public ActionResult Index(string isJSON = "no")
  {
  
      /// ...All the code from before...
  
      if (isJSON == "yes")
      {
          return Json(i, JsonRequestBehavior.AllowGet);
      }
      else
      {
          return View(i);
      }
  
  }
  </code></pre>
  
  <p>Then, when I want the JSON version, in my AJAX request I specify the URL as <code>/MyController/Index/?isJSON=yes</code></p>
  
  <p>When I want my pretty table view, I just use <code>/MyController/</code></p>