Passing Server Side Mvc Variables To Javascript
I am looking at feedback as to the best and easiest way to pass server side variables from a controller action to the sites html markup and for them to be used then by javascript o
Solution 1:
You have a couple of options.
One is attach data-attributes or id's to elements and fetch them using javascript.
Using razor views:
<div id="someid" data-name="@item.attribute"></div>
JS:
$('#someid').data('name')
Or you can render the data directly into a script tag.
Using razor:
var somevar = "@item"
You can also Json.Encode more complex objects.
var somevar = @Html.Raw(Json.Encode(object))
Post a Comment for "Passing Server Side Mvc Variables To Javascript"