2

I have created a view which contains both Javascript and functions as shown:

@functions
{
    public int DaysInMonth(string Month)
    {
         //code to get the no of days in month;
    }
}

I have drop down list that contains month.. Now I want to write code on dropdownlist's change event using jQuery which will call the above function.

<script type="text/javascript">
    $(function () {
        $('#Months').live('change', function () { 
           //code to call the above function  
        });
    });
</script>

Here ('#Months') is the id of the dropdownlist..

Note: Both the above code snippets are in the same view (Index).

How can i link the above two code snippets??

Thank you..

2 Answers 2

2
@functions
{
    public int DaysInMonth(string Month)
    {
         //code to get the no of days in month;
    }
}

try like this

<script type="text/javascript">
    @DaysInMonth(month);
</script>

it worked fine in my program.

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

Comments

1

Try this:

/*@cc_on @*/

<script type="text/javascript">
    $(function () {
        $('#Months').live('change', function () { 
           var monthSelected = $(this).val();
           //code to call the above function 
           @DaysInMonth(monthSelected);
        });
    });
</script>

More info on this is at: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

Also have a look at this answer: JavaScript Error: conditional compilation is turned off in MVC2 View

2 Comments

how to turn on the conditional compilation?
/*@cc_on @*/ this will turn it on

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.