ASP.NET MVC 5 – jQuery AJAX Post to Controller Method Model Parameter

ASP.NET MVC 5 – jQuery AJAX Post to Controller Method Model Parameter

 
   var userdata = new Object();
 
   $(document).ready(function () {
 
      $("#submitbtn").click(function () {
 
         $(".usertxtbox").each(function () {
 
            if ($(this).attr("id") == "firstname") para.firstname = $(this).val();
            else if ($(this).attr("id") == "lastname") para.lastname = $(this).val();
            else if ($(this).attr("id") == "posttitle") para.posttitle = $(this).val();
            else if ($(this).attr("id") == "userid") para.userid = $(this).val();
 
         });
 
         PostObj(userdata);
 
      });
 
   });
 
   function PostObj(data)
   {
 
      $.ajax({
         url: "@(Url.Action("Result", "JSON"))",
         type: "POST",
         data: data,
         dataType: "json",
         success: function (data) {
            console.log(data);
         }
      });
 
   }
 
 
   [HttpPost]
   public JsonResult Result(User coll)
   {
      return Json(coll, "application/json", JsonRequestBehavior.AllowGet);
   }
 
 
   public class User
   {
      public User()
      {
      }
 
      public User(string userid, string firstname, string lastname, string posttitle)
      {
         this.userid = userid;
         this.firstname = firstname;
         this.lastname = lastname;
         this.posttitle = posttitle;
      }
 
      public string userid { get; set; }
      public string firstname { get; set; }
      public string lastname { get; set; }
      public string posttitle { get; set; }
   }
 

Remarks :

For Web API Development with JObject Form Post Parameter, the JavaScript Object Value can be posted directly without Stringify.

 
   $.ajax({
      url: "… …",
      type: "POST",
      data: data,
      success: function (data) {
         console.log(data);
      }
   });
 
 
   public JObject Post(Newtonsoft.Json.Linq.JObject user)
   {
 
   }
 

The Practice for ASP.NET MVC 5 Web Controller Model with jQuery AJAX Post seems different.