0

I'm at the beginner level about mvc apps so I would like some help please.

I'm trying to embed c# with jquery to test a condition of if a user.identity is authenticated on document.load but I'm getting cs1001(Identifier expected).

$(document).load(function () {

            if (@{User.Identity.Name != "Someone"}){
                alert("Something");
            }
        });

1
  • if (@Html.Raw(Json.Encode(User.Identity.Name)) != "Someone") { alert("Something"); } Commented Sep 20, 2017 at 9:58

2 Answers 2

1

Remove curly brackets from if

if (@User.Identity.Name != "Someone"){
    alert("Something");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this:

@if (User.Identity.Name != "Someone"){
   @:alert("Something");
}

or

@if (User.Identity.Name != "Someone"){
   <text>alert("Something");</text>
}

or

var identityName = @Html.Raw(Json.Encode(User.Identity.Name));

if (identityName  != "Someone"}){
    alert("Something");
}

or

if ("@User.Identity.Name" != "Someone"){
    alert("Something");
}

The first 2 are C# if conditions and are computed server side. While the last 2 are javascript if blocks which will be run client side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.