3

Possible Duplicate:
Mix Razor and Javascript code

Here is the code I'm trying to run:

    // Razor code!
    @if (User.Identity.IsAuthenticated) {

        // This is Javascript, NOT C#/Razor code. Hence the '@' symbol.
        var currentUser = @User.Identity.Name;

    } else {

        // Also Javascript code!
        var currentUser = null;
    }

Basically, I want to output this as Javascript if the user is logged in:

var currentUser = @User.Identity.Name;

Or this if the user is logged out:

var currentUser = null;
Community
  • 1
  • 1
sergserg
  • 21,716
  • 41
  • 129
  • 182
  • Check out http://stackoverflow.com/questions/5614941/mix-razor-and-javascript-code – zad Oct 01 '12 at 23:03

3 Answers3

2

As soon as you start tag in Razor, it switches to markup (including <script>). If you don't need any tag, use <text> instead.

If you need quick copy/paste solution, take Tim's or Adam's as both do the job. However, as Razor uses pretty complex algorithms while parsing mix of several languages, it is worth providing Phil Haack's Razor part of quick reference in its entirety (allowed by CC-BY license):

Code Block:

@{ 
  int x = 123; 
  string y = "because.";
}

Expression (Html Encoded):

<span>@model.Message</span>

Expression (Unencoded):

<span>
@Html.Raw(model.Message)
</span>

Combining Text and markup:

@foreach(var item in items) {
  <span>@item.Prop</span> 
}

Mixing code and Plain text:

@if (foo) {
  <text>Plain Text</text> 
}

Mixing code and plain text (alternate):

@if (foo) {
  @:Plain Text is @bar
}

Email Addresses:

Hi philha@example.com

Explicit Expression:

<span>ISBN@(isbnNumber)</span>

Escaping the @ sign:

<span>In Razor, you use the 
@@foo to display the value 
of foo</span>

Server side Comment:

@*
This is a server side 
multiline comment 
*@

Calling generic method:

@(MyClass.MyMethod<AType>())

Mixing expressions and text:

Hello @title. @name.

I left out Razor delegate since there are Razor helpers now.

nrodic
  • 3,026
  • 3
  • 33
  • 38
  • In which way does this answer the OP? – apaderno Oct 01 '12 at 23:09
  • OP asked for switching between contexts. I think my answer is suitable. – nrodic Oct 01 '12 at 23:12
  • In that page, there is a no reference to switching contexts. You should make it clear that what the OP needs is switching contexts, and what exactly the OP needs. – apaderno Oct 02 '12 at 00:03
  • 1
    I don't doubt that. I am just saying that, for who asked the question, or who has the same doubt as the OP, a link like that probably doesn't help much. If the answer would say "see examples 4-6 on this page" that would be more helpful. I would also quote the example, just in the case the page is not accessible to future readers. – apaderno Oct 02 '12 at 00:17
2

Well first you will want to change it to something like (remember your quotes around the @User.Identity.Name):

<script type="text/javascript">
var currentUser = null;
@if (User.Identity.IsAuthenticated) {
    <text>
        currentUser = '@User.Identity.Name';
    </text>
}
</script>

Then add some javascript:

<script type="text/javascript">
if (currentUser != null) {
    document.getElementById('loggedInUser').innerHTML = 'Welcome '+ currentUser;
}
</script>
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
1
<script>

    @if( User.Identity.IsAuthenticated ) {
        <text>var currentUser = "@User.Identity.Name";</text>
    }
    else {
         <text>var currentUser = null;</text>
    }

    alert(currentUser);

</script>

Which produces an output of:

var currentUser = "Foo Bar";
alert(currentUser);

@Adam Plocher's answer shows a cleaner way to represent it (removing the JavaScript variable declaration from the server script).

An even cleaner way to represent it in a single, slightly easier-to-understand line:

<script>
    var currentUser = '@(User.Identity.IsAuthenticated ? User.Identity.Name : "Anonymous" )';
    alert(currentUser);
</script>
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • All the answers has been down-voted. I wonder what is wrong with this one. – apaderno Oct 02 '12 at 00:06
  • My first version could have been better. I believe both of the current samples answer the question and they have both been tested. – Tim M. Oct 02 '12 at 00:10