0

I have following data that I have serialized as JSON from my code behind file.

public class PieModel {
    public string label { get; set; }
    public double data { get; set; }
}

var data = new List<PieModel> {
    new PieModel { label = "Pending", data = 10d }
    new PieModel { label = "New", data = 40d }
    new PieModel { label = "Overdue", data = 50d }
};

hdnData.Value = new JavaScriptSerializer().Serialize(data);

I read this serialized data in JavaScript like this

var tempHdnData = $("#hdnData");

But now I want to iterate on tempHdnData and get label and data members separately in JavaScript code. How can I achieve that?

1 Answer 1

1

You could write your code like this in the code behind:

protected List<PieModel> GetData() {
    return new List<PieModel> {
        new PieModel { label = "Pending", data = 10d }
        new PieModel { label = "New", data = 40d }
        new PieModel { label = "Overdue", data = 50d }
    };
}

And in your webform:

var tempHdnData = <%= new JavaScriptSerializer().Serialize(GetData()) %>

Now you can write

$.each(tempHdnData, function (_, data) {
    console.log(data)
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.