简体   繁体   English

Javascript var作为C#Mvc3剃须刀中的参数

[英]Javascript var as argument in c# mvc3 razor

I'm developing a web in MVC3 razor and I want to send as an argument a javascript variable, but I don't know if this is posible. 我正在MVC3剃须刀中开发一个Web,我想将一个javascript变量作为参数发送,但是我不知道这是否可行。 The code is something like this: 代码是这样的:

function clean(caja) {
    @{
        Inicio.Controllers.kitClass kit = new Inicio.Controllers.kitClass(caja);
        kit.clean();
    }
}

Thanks for your help. 谢谢你的帮助。

No it is not. 不它不是。 you will have to make an ajax call. 您将必须进行ajax调用。

If you want to pass a razor var to a function you can do this: 如果要将剃刀var传递给函数,可以执行以下操作:

@{
    var someItem = "Hello! Welcome to MVC";
}
<script type="text/javascript">
    $(document).ready(function () {
        alert(@someItem );
    });
</script>

I think you might consider something like this as an alternative: 我认为您可以考虑使用以下替代方法:

@{ @ {

var kit = new Test.Controllers.kitClass("something");
Func<bool,bool> Incio = delegate(bool cleanMe) { return kit.clean(); };

} }

<script type="text/javascript">
    $(document).ready(function () {
        executeTheFunc();
    });
    function executeTheFunc() {
       if('@Incio(true)' == 'True'){
            alert("This is clean!");
       } else {
            alert("This is not clean!");
       }
    }
</script>

Mock class - not sure what yours looks like: 模拟课程-不确定您的模样:

public class kitClass {
    public kitClass(string something) {

    }
    public bool clean() { return true; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM