2011-09-25 81 views

回答

1

本:

@{var x = 5;} 

<script type="text/javascript"> 
    var c= @(x); 
    alert(c); 
</script> 
+0

听起来好像他是以相反的方式。 –

+0

感谢您的快速回答,但我想做相反的事情,@(x)= 5 – avi

+0

好的,js是一个客户端代码,只能在浏览器上运行。因此,您可以使用链接@Darin在Q.问候下面评论。 –

0

只需使用@x来获取值。防爆。

@{int x = 42;} 

<script type="text/javascript"> var x = @(x); alert('The answer to the Ultimate Question of Life, the Universe, and Everything is ' + x); </script> 
+0

对不起,我的英文不好,在我的脚本中,我需要更改x变量以便在ActionLink中使用它,像这样:

  • @ Html.ActionLink(“שיחות”,“ClientTalksCheck”,“Talk”,new {id = x} ,null)
  • avi

    0

    你可以做这样的事情...

    @{ // declare this in your view 
        int x = 100; 
    } 
    
        $(document).ready(function() { 
    
        var postdata = @x + 100; // here is the updated code. if you want to change the value eg. this will alert 200 
        alert(postdata); 
        $.ajax({ 
          url: 'Home/Index', 
          data: postdata, 
          success: function (returnData) { 
           alert(returnData); // this will alert 210 
          } 
         }); 
    }); 
    

    控制器

    public ActionResult Index(int x) // x will get the value here which is 200 here now.. 
    { 
        var y = x + 10; // do some logic here with the posted data from your view 
        return Json(y,JsonRequestBehaviour.AllowGet); // you will need to return Json here cos you using the AJAX call 
    } 
    
    +0

    我需要更改x的值,@(x)= 100但它不起作用 – avi

    +0

    您的意思是您想要更改jQuery代码中的x值? like var postdata = @(x)+ 200 //示例 –

    +0

    看看我更新的代码... –