3

i need to get data sent with JSON and save to model in asp.net controller

  //JSON data
        var dataType = 'application/json';
        var data = {
            ID: 'Zaki',                
        }

        console.log('Submitting form...');
        console.log(data);
        $.ajax({
            type: 'POST',
            url: 'Save',
            dataType: 'json',
            contentType: dataType,
            data: data,
            success: function (result) {
                console.log('Data received: ');
                console.log(result);
            }
        });
          

Controller

 [HttpPost]
    public ActionResult Save([FromBody] string ID)
    {
       
        return Json (ID);


    }






          

am getting null in console , it supposed to be zaki and from there i wanna write saving code...

enter image description here

5
  • Are you trying to get data from you DB and wanted to bind with Model to display on your console? It is bit unclear with your question. Commented Dec 24, 2018 at 7:34
  • no am trying to get data from JSON and save it to DB Commented Dec 24, 2018 at 7:36
  • If i am not wrong, your json should look like this. var data = {"ID": "Zaki", } Commented Dec 24, 2018 at 7:51
  • i added a screenshot , i think the problem is not in sending is getting data Commented Dec 24, 2018 at 8:05
  • Then Create a Model for your Json and use JsonConvert to Serialize like this : Newtonsoft.Json.JsonConvert.SerializeObject(NewlyCreatedJsonModel)) Commented Dec 24, 2018 at 8:09

3 Answers 3

3

Another way to do it is to simply use 'dynamic' type to handle json request data. Take a look:

[HttpPost]
public IActionResult YoutMethod([FromBody] dynamic requestData)
{
  Log.Information(requestData.field1);
  Log.Information(requestData.field2);
  // ...

  return Ok();
}
Sign up to request clarification or add additional context in comments.

Comments

2

Modify this line in your code data: data, to

data:JSON.stringify(data)

When sending data to a web server, the data has to be a string and JSON.stringify method converts a JavaScript object into a string.

Another approach would be, instead of getting raw string value, wrap your parameter into a class object like this

public class ParamObj
{
public string ID{get;set;}
}

and in your controller get a parameter of this object type like this..

public ActionResult Save([FromBody] ParamObj data)

Thanx

Comments

0

I know that is already marked as answered, but here is another way to do it:

I am not using the binding FromBody attribute.

Controller

    public class JsonRequest
    {
        public string Id { get; set; }
    }

    [HttpPost]
    public ActionResult Save(JsonRequest data)
    {

        return Json(data.Id);
    }

Instead of using dataType I am using accept and you don't need to convert your json into a string.

To avoid problems with relative paths I am using: url: '@Url.Action("Save", "Home")' as well.

Javascript

function send()
{
    //JSON data
    var dataType = 'application/json';
    var data = {
        "id": "Zaki"
    }

    console.log('Submitting form...');
    console.log(data);
    $.ajax({
        type: 'POST',
        url: '@Url.Action("Save", "Home")',                        
        accept: dataType,
        data: data,
        success: function (result) {
            console.log('Data received: ');
            console.log(result);
        }
    });
}

Good luck with your project.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.