ozz
ozz

Reputation: 5366

Can an MVC @helper be called from Javascript?

As per the title, can an MVC @helper function be called from javascript/JQuery?

@helper DoStuff(string someValue) {

         //do some stuff   
        }

then later on:

$("#MyDropdown").on("change", function() {

        @DoStuff('blah')

    });

Upvotes: 1

Views: 1660

Answers (1)

Joseph Woodward
Joseph Woodward

Reputation: 9281

No, it can't.

The reason for this is because ASP.NET MVC parses the Razor Syntax and compiles it down to C#; where was the Javascript is executed by the visitor's browser.

If you wish to execute a Razor helper like you demonstrate in your question then you're going to need to perform an AJAX call to a controller action and render the helper's output to HTML.

Upvotes: 3

Related Questions