1

I will get JSON objects like below formats,each format contains differnt properties/Keys i.e. these properties/keys are dynamic.

var SelectedRows=   [
            {"id":"1","name":"jhon","phone":"6699"},
            {"id":"2","name":"Aron","phone":"7799"},
            {"id":"3","name":"Iyan","phone":"8899"},
        ]

var SelectedRows=   [
            {"id":"1","fname":"jhon","lname":"6699"},
            {"id":"2","fname":"Aron","lname":"7799"},
            {"id":"3","fname":"Iyan","lname":"8899"},
        ]

var SelectedRows=   [
        {"id":"1","fname":"jhon","lname":"6699"},
        {"id":"2","fname":"Aron","lname":"7799"},
        {"id":"3","fname":"Iyan","lname":"8899"},
        ]

Posting the data using AJAX post call.

$.ajax({
    url: "@Url.Action("Export", "Export")",
    type: "POST",
    data: JSON.stringify(SelectedRows),
    success: function(result) {}
});​

Controller Action method:

[HttpPost]
public ActionResult Export(**what type to specify here**)
{

}

As i said properties/keys are dynamic so i can't use the strong type model here. I have to specifiy some genaric type of parameter for the action method.

I don't it is possible or not? if yes, how to do?

palaѕн
  • 72,112
  • 17
  • 116
  • 136
User_MVC
  • 251
  • 2
  • 7
  • 21

3 Answers3

0

Sound like a dynamic type may be the answer for you. A full detail post can be found here.

Community
  • 1
  • 1
Quinton Bernhardt
  • 4,773
  • 19
  • 28
  • I tried that dynamic type, but it always showing the value of parameter as {object}. and your link explaining about deserialization of json value after receiving in action method. – User_MVC Jan 02 '13 at 11:49
0
[HttpPost]
public ActionResult Export(FormCollection collection)
{

}

More info about FormCollection.

ZippyV
  • 12,540
  • 3
  • 37
  • 52
0

This is how i done.

$.ajax({
    url: "@Url.Action("Export", "Export")",
    type: "POST",
    data: JSON.stringify(SelectedRows),
  **contentType: "application/json; charset=utf-8",**
    success: function(result) {}
});​

Need to specify the application contentType while the posting the data and receive the list values of dynamic type.

[HttpPost]
public ActionResult Export(**List<dynamic>** SelectedRows)
{

}
spender
  • 117,338
  • 33
  • 229
  • 351
User_MVC
  • 251
  • 2
  • 7
  • 21