I’m using asp.net mvc 3-4.0 and the question is how to pass a model value to a .js file. Sometimes i don’t want to put too many jquery lines on my razor view page.
something likes
$("#qcontent").load(
"@Url.Action("QuestionList", "TuongTac")",
{
id: 1,
keywords: @Model.keyword
....
}
is it possible to make it work in a .js file ?
Thanks.
The cleanest way I’ve found to do this is to use
data-*attributes.In you Razor view:
Then in your external JavaScript file:
Another option is to put the procedural JavaScript in an external file, and then dump out just the settings required in one hit in your Razor view:
and refer to it in your JavaScript file:
The downside of this approach is that you are polluting the global namespace, so you’ll want to be careful about namespacing.