ferpinan
ferpinan

Reputation: 171

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.

Upvotes: 0

Views: 620

Answers (2)

anAgent
anAgent

Reputation: 2780

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

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

No it is not. you will have to make an ajax call.

Upvotes: 3

Related Questions