简体   繁体   中英

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. 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.

If you want to pass a razor var to a function you can do this:

@{
    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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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