I would like to access ViewBag data from within client side code. This is what I've tried in my controller:
ViewBag.Tasks = new JavaScriptSerializer().Serialize(tasks);
In my view I would then put this in a hidden field so that it available client side:
<input type="hidden" name="diagramData" data-nodes="@ViewBag.Tasks" />
In my Javascript file, I would then search for the info related to the selected node:
alert($("#diagramData").data("nodes")); //?????
The alert always shows "undefined". Why is this?
That's the wrong jQuery selector to retrieve the element. Try:
$('input[name="diagramData"]').data("nodes")
You set the name
attribute in the HTML, yet you try to select it by id
(because of the #
). The attribute-equals selector is required to select by name
.
Or, of course, just add an id
parameter as "diagramData" and use your original selector.
Reference:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.