title: Asp.net MVC passing data from one method to another method then to a view
body: <p>I am confused by passing data from one <code>ActionResult</code> to another <code>ActionResult</code> method. </p>
  
  <p>What this is about let me describe,</p>
  
  <p>I need to pass value from one method to another, and that value must be available on view that I'm rendering from that another method.</p>
  
  <p>What I saw in my project is this (this is Edit but [<code>HttpPost</code>] Edit,  and it is redirecting to Edit also but with [<code>HttpGet</code>] insted of [<code>HttpPost</code>]):</p>
  
  <pre><code>TempData["Success"] = True;
  return RedirectToAction("Edit/" + customer.Id, "Customer");
  </code></pre>
  
  <p>And what is done on 
  [HttpGet] Edit :</p>
  
  <pre><code> if (TempData["Success"] != null &amp;&amp; TempData.ContainsKey("Success"))
   ViewBag.Success = Convert.ToString(TempData["Success"]);
   return View(model);
  </code></pre>
  
  <p>As you can see guys on [<code>HttpPost</code>] <code>TempData["Success"]</code> is set to <code>True</code>;
  And Redirection is made to [<code>HttpGet</code>] method and there is written next code:</p>
  
  <pre><code>if (TempData["Success"] != null &amp;&amp; TempData.ContainsKey("Success"))
   ViewBag.Success = Convert.ToString(TempData["Success"]);
   return View(model);
  </code></pre>
  
  <p>So I am wondering why it's needed to set <code>TempData</code> and later based on value of that <code>TempDate</code> let's set value to a <code>ViewBag</code>, can't I just set value of ViewBag on my first ActionResult before redirection so it can be available also on a View even if view is being rendered/called from <code>HttpGet</code> action result? </p>
  
  <p>For example:</p>
  
  <p>instead of this:</p>
  
  <pre><code>TempData["Success"] = True;
  return RedirectToAction("Edit/" + customer.Id, "Customer");
  </code></pre>
  
  <p>Can I simply write in my <code>HttpPost</code></p>
  
  <pre><code>ViewBag.Success = True;
  return RedirectToAction("Edit/" + customer.Id, "Customer");
  </code></pre>
  
  <p>Or this is needed to be done with <code>TempData</code> because <code>ViewBag</code> wouldn't be available on a View if I don't put value in it on a ActionResult which is redirecting to a View, and in this case it is HttpGet and not HttpPost (so that means I need to set ViewBag value on a HttpGet?)</p>
  
  <p>And If I must do it this way, I could also use two ViewBags, and not ViewBag and TempData?</p>
  
  <p>And why would someone solve it like this at all? Is this correct approach or what?</p>