title: Passing data from view to controller using jquery
body: <p>Im trying to pass user input from html to the controller and I am using jquery and ajax.I have followed this tutorial  <a href="https://www.c-sharpcorner.com/blogs/post-the-data-to-asp-net-mvc-controller-using-jquery-ajax" rel="nofollow noreferrer">https://www.c-sharpcorner.com/blogs/post-the-data-to-asp-net-mvc-controller-using-jquery-ajax</a>.</p>
  <p>Here is the controller code.</p>
  <pre><code> public class JQueryController
  
      {
          // GET api/&lt;controller&gt;
          public ActionResult Index ()
          {
              return View(); 
          }
  
  
       
          [HttpPost]
          public JsonResult AjaxPostCall(Userinfo userdata)
          {
              Userinfo userinfo = new Userinfo
              {
                  Name = userdata.Name,
                  EmailAdress = userdata.EmailAdress,
                  Phone = userdata.Phone
  
              };
  
              return JSON(userinfo, JsonRequestBehavior.AllowGet);
          }
  </code></pre>
  <p>Here is the code in the cshtml file</p>
  <pre><code>@{
      @Shakesphere.Controller.JQueryController.UserInfo;
      @Model.Shakesphere.ViewModels.UserDataModel
  
  
  }
  
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
      &lt;title&gt;&lt;/title&gt;
  
      &lt;script type=&quot;text/javascript&quot;
              src=&quot;https://code.jquery.com/jquery-1.12.4.js&quot;&gt;&lt;/script&gt;
      &lt;script&gt;
          (function CheckInput() {
  
              var userInfo = new Object();
              userInfo.Name = $('name').val();
              userInfo.EmailAddress = $('email').val();
              userInfo.Phone = $('phone').val();
              if (UserInfo != null) {
  
                  $.ajax({
                      type: 'POST',
                      url: '/JqueryAjaxCall/AjaxPostCall',
                      data: JSON.stringify(UserInfo),
                      contenttype: &quot;application.json; charset=utf-8&quot;,
                      datatype: &quot;json&quot;,
                      success: function (response) {
                          if (response != null) {
                              alert(&quot;Name:&quot; + response.Name);
                          }
  
                          else {
                              alert(&quot;Something went wrong &quot;)
                          }
  
  
  
                      },
  
                      failure: function (response) {
                          alert(response.responseText);
                      },
                      error: function (response) {
                          alert(response.responseText);
                      }
  
  
                  }
  
                  )
  
              }
  
  
  
          }
  
      &lt;/script&gt;
  
  </code></pre>
  <p>THe errors I get is that view and json don't exisit in current context.  Also I get an error  which is
  Error   CS0117  'JQueryController' does not contain a definition for 'UserInfo' .</p>