|
1 | | -<!-- default badges list --> |
2 | | - |
3 | | -[](https://supportcenter.devexpress.com/ticket/details/E3338) |
4 | | -[](https://docs.devexpress.com/GeneralInformation/403183) |
5 | | -[](#does-this-example-address-your-development-requirementsobjectives) |
6 | | -<!-- default badges end --> |
7 | | - |
8 | | -# ASP.NET MVC - How to pass complex objects to a callback Action as callback arguments |
9 | | - |
10 | | -This example demonstrates how to convert custom types to a JSON string on the client and parse this string on the server. |
11 | | - |
12 | | -The conversion is performed using the `$.toJSON` function that is declared in the [jquery.json-2.2.min](https://github.com/krinkle/jquery-json) file. |
13 | | - |
14 | | -```jscript |
15 | | -function GetSerializationData() { |
16 | | - var editorsNames = ["Name", "Age", "Email", "ArrivalDate"]; |
17 | | - var editorsValues = {}; |
18 | | - for (var i = 0; i < editorsNames.length; i++) { |
19 | | - var control = ASPxClientControl.GetControlCollection().GetByName(editorsNames[i]); |
20 | | - editorsValues[control.name] = control.GetValue(); |
21 | | - } |
22 | | - return $.toJSON(editorsValues); |
23 | | -} |
24 | | -``` |
25 | | - |
26 | | -The `JavaScriptSerializer.Deserialize` method parses the JSON string in a Controller's Action. |
27 | | - |
28 | | -```csharp |
29 | | -MyData GetSerializationObject() { |
30 | | - string jsonText = Request.Params["SerializationData"]; |
31 | | - if (string.IsNullOrEmpty(jsonText)) |
32 | | - return null; |
33 | | - MyData data = null; |
34 | | - try { |
35 | | - data = new JavaScriptSerializer().Deserialize<MyData>(jsonText); |
36 | | - } catch { |
37 | | - ViewData["ErrorMessage"] = "Incorrect data"; |
38 | | - } |
39 | | - return data; |
40 | | -} |
41 | | -``` |
42 | | - |
43 | | -In this example, if it is not possible to deserialize user input, an exception is thrown. |
44 | | - |
45 | | -## Files to Review |
46 | | - |
47 | | -* [HomeController.cs](./CS/DevExpress.Razor/Controllers/HomeController.cs) |
48 | | -* [CallbackPanelPartial.cshtml](./CS/DevExpress.Razor/Views/Home/CallbackPanelPartial.cshtml) |
49 | | -* [EditorsPartial.cshtml](./CS/DevExpress.Razor/Views/Home/EditorsPartial.cshtml) |
50 | | -* [Index.cshtml](./CS/DevExpress.Razor/Views/Home/Index.cshtml) |
51 | | -<!-- feedback --> |
52 | | -## Does this example address your development requirements/objectives? |
53 | | - |
54 | | -[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=asp-net-mvc-convert-custom-types-to-from-json&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=asp-net-mvc-convert-custom-types-to-from-json&~~~was_helpful=no) |
55 | | - |
56 | | -(you will be redirected to DevExpress.com to submit your response) |
57 | | -<!-- feedback end --> |
| 1 | +<!-- default badges list --> |
| 2 | + |
| 3 | +[](https://supportcenter.devexpress.com/ticket/details/E3338) |
| 4 | +[](https://docs.devexpress.com/GeneralInformation/403183) |
| 5 | +[](#does-this-example-address-your-development-requirementsobjectives) |
| 6 | +<!-- default badges end --> |
| 7 | + |
| 8 | +# ASP.NET MVC - How to pass complex objects to a callback Action as callback arguments |
| 9 | + |
| 10 | +This example demonstrates how to convert custom types to a JSON string on the client and parse this string on the server. |
| 11 | + |
| 12 | +The conversion is performed using the `$.toJSON` function that is declared in the [jquery.json-2.2.min](https://github.com/krinkle/jquery-json) file. |
| 13 | + |
| 14 | +```jscript |
| 15 | +function GetSerializationData() { |
| 16 | + var editorsNames = ["Name", "Age", "Email", "ArrivalDate"]; |
| 17 | + var editorsValues = {}; |
| 18 | + for (var i = 0; i < editorsNames.length; i++) { |
| 19 | + var control = ASPxClientControl.GetControlCollection().GetByName(editorsNames[i]); |
| 20 | + editorsValues[control.name] = control.GetValue(); |
| 21 | + } |
| 22 | + return $.toJSON(editorsValues); |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +The `JavaScriptSerializer.Deserialize` method parses the JSON string in a Controller's Action. |
| 27 | + |
| 28 | +```csharp |
| 29 | +MyData GetSerializationObject() { |
| 30 | + string jsonText = Request.Params["SerializationData"]; |
| 31 | + if (string.IsNullOrEmpty(jsonText)) |
| 32 | + return null; |
| 33 | + MyData data = null; |
| 34 | + try { |
| 35 | + data = new JavaScriptSerializer().Deserialize<MyData>(jsonText); |
| 36 | + } catch { |
| 37 | + ViewData["ErrorMessage"] = "Incorrect data"; |
| 38 | + } |
| 39 | + return data; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +In this example, if it is not possible to deserialize user input, an exception is thrown. |
| 44 | + |
| 45 | +## Files to Review |
| 46 | + |
| 47 | +* [HomeController.cs](./CS/DevExpress.Razor/Controllers/HomeController.cs) |
| 48 | +* [CallbackPanelPartial.cshtml](./CS/DevExpress.Razor/Views/Home/CallbackPanelPartial.cshtml) |
| 49 | +* [EditorsPartial.cshtml](./CS/DevExpress.Razor/Views/Home/EditorsPartial.cshtml) |
| 50 | +* [Index.cshtml](./CS/DevExpress.Razor/Views/Home/Index.cshtml) |
| 51 | +<!-- feedback --> |
| 52 | +## Does this example address your development requirements/objectives? |
| 53 | + |
| 54 | +[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=asp-net-mvc-convert-custom-types-to-from-json&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=asp-net-mvc-convert-custom-types-to-from-json&~~~was_helpful=no) |
| 55 | + |
| 56 | +(you will be redirected to DevExpress.com to submit your response) |
| 57 | +<!-- feedback end --> |
0 commit comments