views:

146

answers:

2

basically what the title says, i know you can return a javascript view but this has never worked for me.

i couldn't find a proper tutorial or explanation about this concept either, if anyone can shed some light on it that would be great (yep, ive tried google countless times).

thanks

edit: ive used your tips, debugger etc... thanks guys. wow! i just noticed something, every time i try to call a function, no matter what function, in javascript, somehow jquery and ms ajax framework javascript captures it and checks if the document is ready (document.onready or other) and never returns the control back to the function im calling! why on earth is it doing this? i've never asked for it to!!!

all i have is references to these libraries, script/link references as you do on the top of your master page.

thsi is ridiculous, anyone have any ideas?

+1  A: 

Very simple:

1) Create a View for JavaScript call:

HelloWorld.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script type="text/javascript">
    alert('Hello world !!!')
</script>

2) Then create an Action for this View:

MainController.cs

[ChildActionOnly]
public ActionResult HelloWorld()
{
    if (It_Is_Time_To_Call_HelloWorld_On_ClientSide)
    {
        return View();
    }

    return new EmptyResult();
}

3) Add RenderAction for your JavaScript call Action somewhere at the bottom of your page

Site.master

    ...

    <% Html.RenderAction("HelloWorld", "Main"); %>
    </body>
</html>

Hope this helps

eu-ge-ne
thanks you, i tried this, check out my edit, jquery and msajax keep intercepting my functions calls. was wondering if you know another way to call function from server as in using the return javascripResult method using mvc?
Erx_VB.NExT.Coder
+1  A: 

I've never had an issue with JQuery like that, but for testing purposes you could try using this in a view:

$(document).ready(function() {
    alert("Here");
});

And an action method:

public void DoThis()
{
   return View();
}

Just to test the code working. Nothing special, must a view with this alert and an action method in any controller.

Does that work?

Brian
thanks mate, turns out i was calling a js function like this...FunctionName;instead of FunctionName();
Erx_VB.NExT.Coder