开发者

asp.net mvc3 razor, working on view variable with javascript

开发者 https://www.devze.com 2023-03-28 05:06 出处:网络
Is it possible to initialize a variable 开发者_开发技巧in view using razor and then manipulate the value of it using jquery or vice versa? Yes,

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

0

精彩评论

暂无评论...
验证码 换一张
取 消