0

I have a model with a method defined as:

public bool isQuantityInRange(int qty){}

But I need to access this from a JavaScript parser in a method similar to the following:

<input id="QuantityTextBox" name="quantity" type="number" value="@Model.Quantity" style="width: 50px" />

@using (Script.Foot())
{
    <script type="text/javascript">

        var validateQuantity = function () {

            if ([email protected]($("#QuantityTextBox").val())) {
                // do some stuff
            else {
                // do different stuff
        }

        $("#QuantityTextBox").change(validateQuantity);
        validateQuantity();
</script>

Granted, I can't access the value of the textbox from inside the cs statement. How do I do something like this?

2
  • 5
    Javascript runs in your client's browser. C# runs on the web server. To call a C# function from javascript, you would have to ajax the call. If isSquantityInRange() isn't very complicated, it'd probably make more sense to just reimplement it in javascript. (If it was complicated, you would make an ajax call to the function, then handle the response in the success handler.) Commented Jul 7, 2014 at 21:30
  • possible duplicate of MVC3 Calling controller method from Javascript (Though I realized after I flagged, that that isn't an exact dup, in that it describes how to ajax-call a serverside function, but that question's function doesn't take any parameters. Quite similar, though - just pass in parameters using the "data" parameter in the ajax call.) Commented Jul 7, 2014 at 21:31

0

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.