title: Returning Json Data From Controller to View ASP.NET MVC
body: <p>I am trying as the title says to return a Json message from the Controller to the View after it validates.</p>
  
  <p>I have made a breakpoint, and I know that the code works from Controller side, and that my JavaScript calls with success the ActionResult now. How do I display that message in the View?</p>
  
  <p>There are two buttons, stamp in and stamp out. If the user stamps in twice, it should get a message, same with stamp out. I have two ActionResults who are indentical except some message and string changes.</p>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
      public ActionResult CreateStamp(Stamping stampingmodel)
      {
          var validateMsg = "";
          stampingmodel.Timestamp = DateTime.Now;
          stampingmodel.StampingType = "in";
  
          if (stampingmodel.User == null || ModelState.IsValid)
          {
              var idValidated = db.Users.Find(model.UserId);
  
              if (idValidated != null)
              {
                  var stamp =
                      db.Stampings.Where(s =&gt; s.UserId == stampingmodel.UserId)
                          .OrderByDescending(s =&gt; s.Timestamp)
                          .FirstOrDefault();
  
                  if (stamp.StampingType == stampingmodel.StampingType)
                  {
                      if (stampingmodel.StampingType == "in")
                      {
                          validateMsg = "Stamped Twice In A Row!";
                      }
                  }
  
                  else
                  {
                      if (stampingmodel.StampingType == "in")
                      {
                          validateMsg = "Stamped In, Welcome.";
                      }
                  }
              }
  
              db.Stampings.Add(stampingmodel);
              db.SaveChanges();
          }
  
          return Json(new {Message = validateMsg });
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>$(document).ready(function () {
  
  $("#stampInBtn").click(function () {
  
      var userId = $("#userId").val();
  
      $.ajax({
          url: "ComeAndGo/CreateStamp",
          type: "POST",
          dataType: "json",
          data: {
              userId: userId,
          }
      });
  
  });
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>    &lt;input type="text" id="idUser" class="form-control" /&gt;
      &lt;br /&gt;
      &lt;input type="submit" value="IN" id="stampInBtn" /&gt;
  </code></pre>
  
  <p>I have more code inside the View of course; divs, head, body, title and scripts. But it's perhaps a little irrelevant.</p>
  
  <p>What should I do to successfully show those messages?</p>
  
  <p>Regards.</p>