asp.net mvc3 razor, working on view variable with javascript
Is it possible to initialize a variable 开发者_开发技巧in view using razor and then manipulate the value of it using jquery or vice versa?
Yes,
If you are in a .cshtml view you can do something like this:
<script type="text/javascript">
var test = '@Model.Variable';
$("#element").val(test);
</script>
But I am not sure if there is a way to pass a JS variable to Razor code, I don't think so. But you could maybe make an ajax call and pass the JS value.
<script type="text/javascript">
var val = $("element").val();
$.ajax({
url: '/controller/action',
data: { id: val },
success: function(result) {
// Do something with the ajax call result here
}
});
</script>
There is also a nice open source project called RazorJS, you should check it out
http://john.katsiotis.com/blog/razorjs---write-razor-inside-your-javascript-files
精彩评论