Andrew Simpson
Andrew Simpson

Reputation: 7324

Ignoring @RenderBody()

I am not sure if i am approaching this correctly.

I have a asp.net mvc web app. There are 4 main 'pages' By clicking on a menu choice a page is selected and I store that page choice within local storage.

Now if I do a page refresh I have written a simple JS script that will maintain the currently viewed page.

But, i have noticed that the @RenderBody() is called 1st before my JS script so that the Home page very briefly appears before the selected page is displayed.

So, in sudo code I want this:

<div id="mainContent" style="height:423px; ">
    if (pageselected!='HOME')
    {
         //call my JS script to load a partial view
    }
    else
    {
        //else load home page
        @RenderBody()
    }
</div>

Have I got my approach correct and how can i o this in the way I have described?

Upvotes: 0

Views: 74

Answers (1)

Andrei
Andrei

Reputation: 56716

Let's make an important distinction here. RenderBody is run on a server side, when the view is being rendered by the server. And js is run on a client, after server is done rendering and sent the page back to the browser. So it will always be that the RenderBody is run before js, and there is no way to change this. Thus the pseudo code you've written is pretty much impossible to implement.

That said, there are always options to have the functionality you need in some other way. Couple of options include:

  1. Decide what to render on a server side. Would be a big overhead to have this page selection stored in a session for example? This will also give you an added bonus of not sending html markup that won't be displayed to the user, which saves some traffic and a handful of browser rendering moments.

  2. Hide everything in the markup by default, just as easy as doing display: none, in your js decide what to show and display this with one line of js/jquery. Perhaps a bit easier to implement in your current code.

Upvotes: 2

Related Questions