0

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.

1
  • I've updated my example to show you can do this using an Encapsulated method. Commented Mar 2, 2012 at 17:42

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

This is the other way round. He wants to send a javascript variable to a .NET instance method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.