Calling ASP.NET MVC Action Methods from JavaScript

后端 未结 8 1346
谎友^
谎友^ 2020-11-29 23:45

I have sample code like this:

 
      
8条回答
  •  执念已碎
    2020-11-30 00:00

    If you want to call an action from your JavaScript, one way is to embed your JavaScript code, inside your view (.cshtml file for example), and then, use Razor, to create a URL of that action:

    $(function(){
        $('#sampleDiv').click(function(){
            /*
             While this code is JavaScript, but because it's embedded inside
             a cshtml file, we can use Razor, and create the URL of the action
    
             Don't forget to add '' around the url because it has to become a     
             valid string in the final webpage
            */
    
            var url = '@Url.Action("ActionName", "Controller")';
        });
    });
    

提交回复
热议问题