1

In the MVC View code below, isLoggedInDb is underlined in green with a tooltip displaying Expected ';'. There clearly is a ;, and it is hard to Google for ; on top of it. I've seen that others have problems creating JavaScript serverside in this manner, so maybe it's not best practice. Should this be ignored, or is there a legitimate reason for Intellisense to complain?

@* SHTML Code Above *@

<script type="text/javascript">
    @{  
        string isLoggedInDb;

        if(Session["isLoggedInDb"] != null)
        {
            if(Session["isLoggedInDb"].ToString() == "1") 
            {
                isLoggedInDb = "1";
            }
            else
            {
                isLoggedInDb = "0";
            }
        }
    }

    var dblogin=@(isLoggedInDb);

    @*....etc*@

    }

Edit:

It just occurred to me that building JavaScript programatically, probably is not the best idea, since it might be considered best practice to keep JavaScript in separate files, which are often cached. I think I should program hidden variables in the HTML that JavaScript reads instead. Maybe someone can confirm this.

3 Answers 3

2

I'm not 100% sure, but I would think the compiler is viewing it as C# code because you have it in a Razor code block @{ }

Try this:

<script type="text/javascript">
    string isLoggedInDb;

    @if(Session["isLoggedInDb"] != null)
    {
        if(Session["isLoggedInDb"].ToString() == "1") 
        {
            <text>isLoggedInDb = "1";</text>
        }
        else
        {
            <text>isLoggedInDb = "0";</text>
        }
    }
</script>

As for building JS on the server, I personally can't stand the idea (I don't like merging any client/server/css code). I can't speak to its performance but I can say you are setting yourself up for a nightmare maintenance scenario keeping track of where all your JS is coming from, not to mention you lose the ability to easily debug and manage your scripts.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice. Jason's method worked, and I tried the above but as long as c# was inside js, intellisense was wacked out. I will try to avoid this technique though like you say.
2

Without a doubt think you should have the approach of taking something similar to:

var isLoggedOn = @Model.IsLoggedInDb

IsLoggedInDb should be part of your ViewModel, your View should not be checking the session, this is very brittle, not easily testable, and the controllers purpose is to orchestrate the data for the presentation layer (view)

3 Comments

Thanks, I guess I was in a hurry and did it the first way that came to mind instead of the best way. I'll give it a try.
Upon further thought, this code is inside a parent layout, and I'm not sure how to assign a model to this. I'm just getting used to assigning models to regular pages. Is there a simple guide to how I'd assign a model to a master layout?
In that case simply use RenderAction as part of your layout. Your views remain completely unaware of a session object and you keep to the clean MVC pattern
1

The code should work as is, however the Razor Parser is fighting the javascript intellisense, so you might want to move all your C# code logic out of the script tags since it doesnt need to be inside, like the following:

@{  
        string isLoggedInDb;

        if(Session["isLoggedInDb"] != null)
        {
            if(Session["isLoggedInDb"].ToString() == "1") 
            {
                isLoggedInDb = "1";
            }
            else
            {
                isLoggedInDb = "0";
            }
        }
    }

 @*....etc*@

<script type="text/javascript">

    var dblogin=@(isLoggedInDb);


..etc..
</script>

1 Comment

You are exactly right. Very straightforward. I didn't think moving the C# code above <script> would work as easily as it did. Thanks.

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.