Biki
Biki

Reputation: 2588

Rendering Raw HTML data from a dynamically populated Json object

I have a complex Json data coming as a string being part of Model.

On Document ready I am parsing some JSON into a Javascript object with the help of following code:

    JSON.parse($('#myData').val());

I am evaluating some condition in the JS function & getting a raw HTML string (something like : <h2><strong>Note :</strong> No Data found for the selection made</h2> )

I want to make the div visible & plugin this code in @Html.Raw() that is present in my HTML body after the above statement gets evaluated.

For example:

<div id="divNoDataFoundMessage" style="display: none">
     <span class="" style="padding-right: 100px;">
      @{ 
           @Html.Raw()
       }
    </span>
 </div>

Please help me how I can substitute the data from document.ready to the above code to display it dynamically.

<input type="hidden" value="@Model.MyListData"  id="myData"/>

 $(document).ready(function () {
        var jsonData = JSON.parse($('#myData').val());

        if (jsonData.NoDriversFound != "") {
            $("#divNoDataFoundMessage").show();
            $("#divNoDataFoundMessage span").text(jsonData.NoDriversFound);
        }
};

Upvotes: 0

Views: 2372

Answers (1)

Jatin patil
Jatin patil

Reputation: 4288

To Place Html Dynamicaly inside div try as follows:

$("#divID").html(JSON.parse($('#myData').val()));

Upvotes: 1

Related Questions