skip to Main Content

the result of content type : "application/x-www-form-urlencoded" stored in Request.Form in Asp MVC.

but for "application/json" i cant find the store location

here is the code that i use:

ajax part

    // reading form data
    var form = $("form")[0];
    var formData = new FormData(form);

    var object = {};
    formData.forEach(function(value, key){
      object[key] = value;
    });
    var data = JSON.stringify(object);


    // ajax part
    // POST application/json; charset=utf-8
    $.ajax({
    type: "POST",
    url: "@Url.Action("FormSubmit","Home")",
    data: data,
    dataType: "json",
    async: true,
    contentType: "application/json",
    processData: true,
    success: function(result) { },
    error: function(result) { }
  });

Controller Part

    [HttpPost]
    public ActionResult FormSubmit(string input1, string input2)
    {
        var httpContext= HttpContext;
        var response = Response;
        var request = Request;
        throw new NotImplementedException();
    }

2

Answers


  1. Chosen as BEST ANSWER

    Found the answer i just need to read post Payload

    here is the code

        [HttpPost]
        public ActionResult FormSubmit()
        {
            Request.InputStream.Position = 0;
            var reqMemStream = new MemoryStream(HttpContext.Request.BinaryRead(HttpContext.Request.ContentLength));
            string reqString = Encoding.ASCII.GetString(reqMemStream.ToArray());
    
            throw new NotImplementedException();
        }
    

  2. You can use controller like this:

    [HttpPost]
    public ActionResult SomeAction(string data)
    {
        List<YOUR_MODEL_CLASS> payloadObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<YOUR_MODEL_CLASS>>(data)(modelData);
        // process your data
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search