1

I have json object rendered as part of view source. I want to convert that json to C# model object and pass that object to a partial view, since partial view is strongly typed to model object.

Eg:

jsonIdentifier = '{"Identifier": { "name":"uno","contextId":"dos"}}'

@{ model.Identifier = Parsing(jsonIdentifier)}
@Html.Partial("_IdentifierPartial", m=>m.Identifier) //where Identifier is the object needs to be passed to  _IdentifierPartial 
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • What have you tried so far? Have you read and tested *[Deserialize JSON with C#](https://stackoverflow.com/q/7895105/3744182)*? – dbc Dec 09 '17 at 18:34
  • @dbc `JavaScriptSerializer` is deprecated, I'd not recommend any answer that uses it – Camilo Terevinto Dec 09 '17 at 18:55
  • @CamiloTerevinto - does that mean there's no longer a good, generic answer for "How do I deserialize JSON in c#"? Or should we be pointing to *[How to Convert JSON object to Custom C# object?](https://stackoverflow.com/q/2246694/3744182)* and *[Deserializing JSON data to C# using JSON.NET](https://stackoverflow.com/q/2546138)* instead? – dbc Dec 09 '17 at 19:17
  • 1
    @CamiloTerevinto Although Microsoft's own documentation recommends using Json.Net, `JavaScriptSerializer` is not deprecated...yet. See [Is the JavaScriptSerializer class deprecated?](https://stackoverflow.com/q/40302416/10263) – Brian Rogers Dec 09 '17 at 20:12
  • @BrianRogers If the creator of the library doesn't use it any more and recommends using another, I consider it dead/deprecated/obsolete. Also, it's not even portable to .NET Core since it's `System.Web` – Camilo Terevinto Dec 09 '17 at 20:32

2 Answers2

1

You should use @Html.Action instead of @Html.Partial. Just pass the jsonIdentifier in as a string parameter and then use Json.NET to convert it into the appropriately type object.

As in..

Output my partial with that Json:
<div>
    @{
        string json = "{Make: \"Toyota\", Model: \"Camry\", Year: 2017}";
    }
    @Html.Action("RenderCarScreen", new { carJson = json });
</div>

Your webmethod would look something like this...

    public ActionResult RenderCarScreen(string carJson)
    {

        Car car = Newtonsoft.Json.JsonConvert.DeserializeObject<Car>(carJson);
        return PartialView("CarScreen", car);
    }
Marcus
  • 166
  • 6
1

We can achieve this with the help of @Html.Action() and @Html.Partial. @Html.Action() requires action method, with the help of action method it rendering the partial page. @Html.Partial() not required action method, based on the object values it rendering the page. So @Html.Partial() gives better performance compare to @Html.Action().

using Newtonsoft.Json;
@{
     string JsonStr = {\"user\":\"me\",\"payment_method\":\"card\",\"items_bought\":[{\"id\":\"001\",\"name\":\"desk\",\"delivered\":\"true\"},{\"id\":\"455\",\"name\":\"chair\","delivered\":\"false\"},{\"id\":\"234\",\"name\":\"mousepad\",\"delivered\":"false\"},{\"id\":\"135\",\"name\":\"cabinet\",\"delivered\":\"false\"}]}
     var JsonObject = JsonConvert.DeserializeObject<MyJSON>(JsonStr);
}
<div>
     @Html.Partial("_IdentifierPartial", JsonObject) 
</div>

The above code will work fine and achieve your requirement. But this approach not advisable, better you can convert the json string to model object in controller side and pass model object to view.

For Ex:

@model ModelClass
<div>
     @Html.Partial("_IdentifierPartial",Model.PartialModel)
</div>

Above approach is rendering the page in browser much faster.