F
F
Faltul2015-11-26 07:40:58
ASP.NET
Faltul, 2015-11-26 07:40:58

Passing arbitrary json to the controller?

How to pass an arbitrary json object to the controller using the POST method in asp.net Mvc?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-11-26
@dmitryKovalskiy

Have you tried sending it as a string variable? type string jsonSource wait in controller method on input? Controllers are strongly typed and bound to variable names. Personally, only generic types like Object, String come to my mind. But in the case of Object - it is necessary to cast the type, but it is not clear what.

D
Dmitry Pavlov, 2015-11-26
@dmitry_pavlov

Something like this on the client side:

$.ajax({
    type: "POST",
    url: "/Controller/Action",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(yourJsonObject),
    ...
});

On the server, the type of the variable yourJsonObject - the parameter of the Action method in the Controller must match the JSON data structure. You can also make it type dynamic - it also seems to work. I don't remember from memory - I'll have to check.
Well, through the line, of course, it is also possible, if it is more convenient for some reason.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question