ASP.NET MVC (razor) using Model in jQuery
I'd like depending of a property of the model (using ASP.NET MVC 3 with razor) in a jQuery function use :
jQuery.datepicker.setDefaults(jQuery.datepicker.regional['xx']);
or
jQuery.datepicker.setDefaults开发者_开发问答(jQuery.datepicker.regional['yy']);
Any idea ?
Thanks,
So you could either put in in a global variable. So in your view, put something like this:
<script language="javascript" type="text/javascript">
var userRegion = '@Model.UserRegion';
</script>
Then in your external script you can just use the userRegion
global variable.
Another approach could be to use the jQuery.data() method.
So in your view, attach the user's region value to any html element - body is a good one. And then you can get it in your external javascript files.
E.g. your view... probably layout page (in which case you should put it in the ViewBag)
<html>
<head>
<!--blah-->
</head>
<body data-user-region="@(string.IsNullOrWhiteSpace(ViewBag.UserRegion) ? "en" : ViewBag.UserRegion)">
<!--blah-->
</body>
</html>
Then you can grab it like so:
var userRegion = jQuery.data(document.body, 'user-region');
jQuery.datepicker.setDefaults(jQuery.datepicker.regional[userRegion]);
精彩评论