How to pass complex JSON object to ASP.net controller

I want to pass a complex JSON object. But when I debug the action of the controller, all virtual properties are zero. Work with ASP.NET, EF and CF.

JSON sends:

POST http://localhost:53214/api/trans/ HTTP/1.1 Pragma: no-cache Content-Type: application/json; charset=utf-8 Host: localhost:53214 Content-Length: 221 { "trans": { "Location": { "locID": 2 } } } 

Trans model:

  public class trans { [Key] public int tID { get; set; } //public int? locID { get; set; } public virtual Location Location { get; set; } } } 

So, when I always send JSON via Fiddler, all Virtual properties are null.

Debugsession

Before I worked with foreign keys, as commented on in the model. It works great.

I want to rebuild the code to optimize the code itself.

How can I initialize properties and deserialize JSON correctly?

Relations Marcus

+6
source share
5 answers

I created a small working sample for you (see solution below). It mainly depends on how you create your client object.

Model -

 public class trans { [Key] public int tID { get; set; } public virtual Location Location { get; set; } } public class Location { public int locID { get; set; } } 

Controller Actions -

  public ActionResult Index() { return View(); } [HttpPost] public JsonResult Submit(trans trans) { return null; } 

Simple view -

 @{ ViewBag.Title = "Home Page"; } <table id="example" class="display"> </table> @section scripts{ <script> $(function() { var o = new Object(); o.tID = 123; o.Location = new Object(); o.Location.locID = 456; $.ajax({ url: '@Url.Action("Submit", "Home")', type: "POST", cache: false, data: JSON.stringify({ trans : o }), contentType: "application/json; charset=utf-8", success: function(data) { if (data) { alert("Success"); } }, error: function(jqXhr, textStatus, errorThrown) { alert(errorThrown); } }); }) </script> } 

When you start the page, it will hit the controller post-installation and check the output file below -

enter image description here

+11
source

You are really near! My answer depends on your name for your model parameter.

Suppose your action is as follows

 [HttpPost] public ActionResult Test(trans model) { return View(); } 

Then your request body will look like this

 { "model.tID":3, "model.Location":{ "locID":2 } } 

Notice how the parameter name matches the name of the JSON keys.

Hope this helps!

+1
source

You can add [FromBody] in the signature of your action:

 public ActionResult SomeAction([FromBody] trans trans) { //Access trans here } 
0
source

resolved. Send the string to Worng Json.

Correct JSON:

 Pragma: no-cache Content-Type: application/json; charset=utf-8 Host: localhost:53214 Content-Length: 144 { "Location": { "locID": 2 } } 

compare with JSON from my Question. As you can see, you do not need to describe the trans model in JSON. Start your JSON object from the contents of the model in which the deserizer is running.

Regards, Marcus Thanks ramiramilu, your JSON String gives me a hint.

0
source

From this post , you need to use json2.js to serialize your json object

  var json = JSON.stringify({'trans':{...}}); $.ajax({ url: '/api/trans', type: 'POST', dataType: 'json', data: json, contentType: 'application/json; charset=utf-8', success: function (data) { $("#target").html(data); } }); 

If this doesn't suit you, you can use the $ plugin . toDictionnary for sending collections of json objects.

  var trans={'trans':{...}};//trans $.ajax({ url: "api/trans", type: "POST", data: $.toDictionary(trans), ... }); 
-1
source

Source: https://habr.com/ru/post/987133/


All Articles