0

I'm trying to work on a clickable DIV from some vertical tab panel. What I want is when clicking on a specific DIV call a static method to do some tasks, so I did this:

<div class="tabbable tabs-left">
<ul class="nav nav-tabs">
  <li onclick="myEvent()"><a href="#b" data-toggle="tab">Tuttle</a></li>

then, my JavaScript code:

 <script>    
      function clickEvet() {    
      alert("alert test message");   
      @MyProject.myMethod()    
    }   
</script>    

Calling the function "clickEvent()" works. The problem is that @MyProject.myMethod() is called no matter what, in other words, @MyProject.myMethod() is being executed as soon the page loads. I want it only when I click on my div.

This is from a cshtml file and I'm using .net 4.5


SOLUTION:

I'm editing my question to post the answer for future references...: Thanks to other comments I finally understood how to work with Ajax and make it work. Here is the solution:

 <script>     
           function vaxGUID() {          
        $.ajax({
            type: 'POST',
            url: "/VAXBean/bmx",
            data: '{"Name":"AA"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'html',
            success: function (data) {                    
                bmx = "http://www.vitalbmx.com";
                $('a.varURL').attr('href', bmx);
                GUID = data;                    
                alert("Good response - " + data + " - " + bmx);
            },
            error: function (data, success, error) {
                alert("Error : " + error);
            }
        });
        return false;
    }
</script>

With this Ajax method I'm making the call to some static method in the background

Rolando F
  • 148
  • 3
  • 17
  • 1
    Are you trying to call C# code from JavaScript directly? – Matti Virkkunen Aug 11 '16 at 20:01
  • 3
    Razor code is executed when the page is loaded, as you noticed. You'll need AJAX for example to do what you want. – Sami Kuhmonen Aug 11 '16 at 20:05
  • Possible duplicate of [How do I call a static method on my ASP.Net page, from Javascript?](http://stackoverflow.com/questions/32871225/how-do-i-call-a-static-method-on-my-asp-net-page-from-javascript) – Heretic Monkey Aug 11 '16 at 21:52

2 Answers2

1

I want it only when I click on my div. <= When you click on the DIV that is being done in the browser after the request has been sent. There is no way for the browser to call directly back inside a method in your application. The HTML has already been generated and sent by the server in the request to the client and that is where that communication cycle stops.

If you want a click (or any other event) to do something specifically on the server you need to do one of these standard actions that are used to communicate back to the server.

  1. Create an AJAX request back to your MVC Controller to get data (or whatever).
  2. Create a link (standard url)
  3. Create a form post back

And of course the @MyProject.myMethod() executes every time your page is rendered because your razor view is a code file that is being interpreted line by line so it can be rendered and sent to the client that requested it. What would be valid here is if myMethod output some javascript or something that the browser could understand and do something with, that is what would be expected.

Igor
  • 60,821
  • 10
  • 100
  • 175
0

You can't do it. All @ (Razor) expressions is resolved during page rendering on server. That's why you method is called.

Probably, you need to make an Ajax call.

Look for a more detailed explanation here: How do I call a static method on my ASP.Net page, from Javascript?

Community
  • 1
  • 1
just.ru
  • 648
  • 5
  • 12
  • 1
    If you find a question which is the same as the current one, please use the flag link under the question to flag it as a duplicate of this one. – Heretic Monkey Aug 11 '16 at 21:52
  • Thanks @just.ru , I wasn't familiar with Razor and wasn't aware about it's behavior....as you suggested, I used Ajax, which works fine now. thanks! – Rolando F Aug 15 '16 at 12:46