Answer the question
In order to leave comments, you need to log in
How to deserialize a request?
The following request comes from the front:
[{"form_name":"form1","value":"buy"},{"form_name":"form2","value":"osago"},{"form_name":" form3","value":"msk"},{"form_name":"form6","value":{"name":"vaprpva","phone":"+7 (999) 999-99-99" ,"date":"02.02.2018","time":"16:00 - 17:00","comment":""}}]
deserialize like this:
ActionResult Test(FormCollection collection){
string request = collection["test"]; //в request вышеуказанный запрос
var req = JsonConvert.DeserializeObject<List<Form>>(request);
public class Form
{
public string form_name { get; set; }
public string value { get; set; }
}
Answer the question
In order to leave comments, you need to log in
There are several options.
The first one is, as written in the post above, to lead to dynamic. But this option has a drawback - at every moment you need to know which field has arrived, otherwise you will get an exception at the runtime level. But to know this, apparently, is not real.
Second, is to convert to
public string form_name { get; set; }
public object value { get; set; }
response.value = response.value as string;
if(response.value == null)
{
response.value = response.value as SomeClass;
if(response.value == null)
{
response.value = response.value as OtherClass;
....
public class Form
{
[PropertyName = "form_name")]
public string FormName { get; set; }
[PropertyName = "value")]
public string Value { get; set; }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question