2

I've been trying to achieve the following:

Call my action (which returns json):

@{ var response = Html.Action("Login", "Auth"); }

And assign my json response to a global javascript variable:

<script type="text/javascript">
    var response = @response;      
    user = response.details;
</script>

However I can't seem to get this to work the way I need it to. The response is not assigned appropriately. Is there an action call which returns json? I know I could use ajax for this, but that's not a valid option since i have to set the global variable before the document.ready is called.

Any tips appreciated...

Kind regards

tereško
  • 58,060
  • 25
  • 98
  • 150
5earch
  • 289
  • 2
  • 4
  • 15
  • What do you mean by "have to set the global variable before the document.ready is called" ? What is that global variable ? I'm almost sure that you we'll be able to do what you want with ajax, but please clarify your need... – Raphaël Althaus Aug 19 '13 at 09:44
  • There is a "JsonResult" return type that you can use in your controller. I suggest you call that with an Ajax post, submit your parameters and retrieve the result as Json. – Rob Aug 19 '13 at 09:44
  • @RaphaëlAlthaus user is a global variable which is defined in a javascript that's included earlier and is not displayed here. I want to retrieve the json and assign it to my user variable before the document.ready gets called and other initializations take place. Therefore ajax is not an option. – 5earch Aug 19 '13 at 09:47

5 Answers5

3

You can use JavaScriptResult directly. See MSDN Doc

public JavaScriptResult Example()
{
    return new JavaScript("var response = 10");
}

Useful liks related to

But I would suggest you to Use $.getJSON() or $.Ajax() instead

Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks for your answer. I'm already returning a JsonResult in my action. That's not the problem. The main problem is that I can't assign my variable 'response' to my global javascript variable user – 5earch Aug 19 '13 at 09:50
  • @user2668680, make a ajax call then, that will be better – Satpal Aug 19 '13 at 09:51
2

Use $.getJSON(), $.post(), $.Ajax() instead, if you want JSON response back

Anand
  • 14,545
  • 8
  • 32
  • 44
  • Thanks for your answer. However jquery/ajax is not an option. I want to retrieve my json and assign it to my javascript before any other javascrip initializations upon document.ready take place. – 5earch Aug 19 '13 at 09:52
  • what values are being returned ?? – Anand Aug 19 '13 at 09:58
1

Instead of trying to use Html.Action like a method call, use Html.RenderAction and use that action to dump any necessary JavaScript to your page. e.g.

AuthController.cs

public class Auth : Controller
{
    /* snip */

    [ChildActionOnly]
    public ActionResult Login()
    {
        return View(/* model? */);
    }
}

~/Views/Auth/Login.cs

<script>
  var auth = @whateverinformation;
</script>

Original View

@{ Html.RenderAction("Login", "Auth"); }
<script>
    user = auth.details;
</script>

Now /Auth/Login can be placed on any page and the content is included at a server level (instead of supplementary with AJAX)


And if that doesn't do it for you, think about making an HTML helper that displays the information instead of trying to use a controller action like a normal method. Something like:

public static IHtmlString Auth_Login(Htmlhelper htmlhelper)
{
    String response;

    /* assign response */

    return new HtmlString(response); /* or maybe MvcHtmlString */
}

Implemented:

<script>
  var response = @Html.Auth_Login();
  user = response.details;
</script>
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

You may check the Channel9 discussion on Evolving Practices in Using jQuery and Ajax in ASPNET MVC Applications.

You may also check the SO question on asp.net MVC3 and jquery AJAX tutorial

Community
  • 1
  • 1
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
0

You should use quotes

<script type="text/javascript">
    var response = "@response";
...
Alex Shkor
  • 1,209
  • 1
  • 10
  • 18