Home » Javascript » Calling javaScript function by a JavaScript class

Calling javaScript function by a JavaScript class

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am a newbie in the ASP.NET technology and also I did not find any answer on the forum (however if there is one please let me know)

I am writing ASP.NET MVC app and I use a lot of Ajax there.
At the moment I am rewriting “free” Javascript into classes and their methods, so the code will be more readable.
And here is the problem:

For such a case:

@Ajax.ActionLink("Text", "SomeAction", "Controller", new AjaxOptions() { UpdateTargetId="content", HttpMethod="Post", OnComplete="javascriptActionStatedBelow"})

    <script>

        function javascriptActionStatedBelow() 
        {
            // do stuff here 
        }

    </script>

Everyting works perfectly fine. However when I try to input the function into the class and then call it, it does not work.

I am doing this in the following way:

    <script>

    // is in seperate file and here is added reference to it
    class myClass {
        javascriptActionStatedBelow() {
            // do stuff here
        }
    }

    // this is on the view page
    var someObj = new myClass();


</script>

I initilize the object in the header and then in the body I put the following.

@Ajax.ActionLink("Text", "SomeAction", "Controller", new AjaxOptions() { UpdateTargetId="content", HttpMethod="Post", OnComplete= "someObj.javascriptActionStatedBelow" })

The second idea does not work.

What am I doing wrong ? Is there a better solution for managing javascript content in ASP.NET MVC applications while using AJAX ?

I know about $.ajax type of solution but actions like OnComplete, OnBegin,.. etc worked perfectly for now.

Answers: