Suraj Shrestha
Suraj Shrestha

Reputation: 1808

Call function created in view using javascript in the same view

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

Upvotes: 2

Views: 188

Answers (2)

ShaunOReilly
ShaunOReilly

Reputation: 2196

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

Upvotes: 1

S.p
S.p

Reputation: 1059

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

Upvotes: 2

Related Questions