I have a requirement to get client side data in json and pass the json in a data attribute in cshtml. Then will read the values from the data attribute.
<div class="container" data-attribute="{Json}">
This above json will contain the data from client side (collected using js).
Any suggestion on how to make this work.
- passing json in data attribute in cshtml
- reading the value from the attribute in cs file
If you want to set the data attribute as
Jsonand you mentioned that it is collected throughJS, then you can use thisJScode to set it to a div.And if you want to retrieve it then you can use this code.
At one point, you mentioned that you want to read the attribute in the
csfile, so I hope you are calling anAjax request, where you can read the data from the attribute as given above and pass it into yourPOST requestand you can read it in cs code.Follow this article to pass
JsonusingAjax.Send JSON data via POST (ajax) and receive json response from Controller (MVC)