title: How to get value from an input control on a View (MVC)
body: <p>I'm working on small mvc application, and on one point I need a value from my Controller's Method,
  and my plan is to store that value in a ViewBag and get it on a View, so I did next:</p>
  
  <pre><code>ViewBag.SuccessBody = "This is a test";
  </code></pre>
  
  <p>And on my Razor View what I did was next:</p>
  
  <pre><code>&lt;input id="customInput" type="hidden" value='@(ViewBag.SuccessBody)' /&gt;
  </code></pre>
  
  <p>//For testing purpose I tried with example below also ..</p>
  
  <pre><code>&lt;input id="customInputAgain" type="hidden" class="form-control" value="@(ViewBag.SuccessBody)"&gt;
  </code></pre>
  
  <p>And when View loads I tried to get a value so I wrote this:</p>
  
  <pre><code> &lt;script&gt;
  
          $(document).ready(function () {
              var customVal = $("#customInput").val();
              var customValAgain = $("#customInputAgain").val();
  
              alert(customVal);
              alert(customValAgain);
  
          });
  &lt;/script&gt;
  </code></pre>
  
  <p>I even tried adding a </p>
  
  <pre><code>alert(customVal.value);
  
  alert(customValAgain.value);
  </code></pre>
  
  <p>But it is not working also, so looks like I am not retrieving values from my Controller/ViewBag that is reason
              why alert allways show empty space - nothing. </p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>I am setting this ViewBag value on a Edit of my article, acttualy on a <code>[HttpPost] EDIT</code>, so when user clicks Save Changes <code>[HttpPost] EDIT</code> method is invoked and when data is probably inserted in a database I am setting ViewBag value and redirecting user to same [HttpGet] Edit method. Here is how that does looks:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(ArticleEditViewModel model)
  {
              if (ModelState.IsValid)
              {
                  //Rest of the code
                  ViewBag.SuccessBody = "This is a test";
                  return RedirectToAction("Edit/" + product.Id, "Article");
              }
              else
              {
                return View("NotFound");
              }
              return View(model);
  }
  </code></pre>
  
  <p>As you can see guys I defined ViewBag but I can not get the value on a View which should be stored in a input control..</p>
  
  <p>Maybe the issue is because I defined a ViewBag in a HttpPost Action method and not in 
  HttpGet where from I'm "opening the view" ?</p>