Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MVC page (C#) - trouble with page layout - header not first

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hi guys,

I am having trouble with MVC here in that a page displays wrong.
Using Visual Web Developer Express 2010, calling the page from my Index page - which looks just fine - by calling an action on home controller which in turn returns the view. Normal, right?

The wrongly displayed page uses lots of C# code, two dynamically created tables with a form button at the end of each row and some functions if a button was pressed.
I am quite certain I am still thinking too much like standard ASP in that case but it should be working nonetheless.
Actually, it does work - but my header appears at the bottom of the page. [3eyes]

The very top of the page code looks like that of every other page:
Code:
@{
    ViewBag.Title = "bla yadda yadda";
}

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title</h1>
                <br/>
                <h2>@ViewBag.Message</h2>
            </hgroup>
        </div>
    </section>
}

@using System.Data
...
...

There is only one _Layout page and one css for all pages.
Funnily, when I look at the source code of the rendered page, the page code begins not with the featured section but with my first "Response.Write" output, while the head block...
Code:
<!DOCTYPE html>
<html lang="de">
    <head>
...
...
...with stylesheet reference, header, navigation, body with featured section and footer come at the end of the file.
Yep, even the freaking body tag comes at the end of the file, after all my body content has been rendered above! [poke]

Anybody else experienced such a thing?

Thanks!
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Little update on this:
After a bit of research I got pointed to adding this:
Code:
@RenderSection("head", required: false)
in the <head> section of the _Layout.cshtml.

Didn't change a thing though. Still, all dynamic content gets rendered before the actual shared layout with head etc. [cry]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Aaaaand I tried to outsmart the system by adding:
Code:
@RenderPage("~/Views/Shared/_Layout.cshtml")
at the top of my messed up view.

The system immediately shunned me for the audacity. [blush]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Got it.

I've experimented around and realised that this happens when writing HTML code via "Response.Write". [elf]
I've replaced all instances with clean html and @variable.
Now it all renders nicely again.

Sorry if I confused the hell out of anyone. [bigcheeks]

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Yeah, remember that response.write is good for debugging but not actual production code. With response.write, you can never be sure where in the document the string will render.
 
Yeah.
And I only did it because I kept getting compiler errors at the last curly brace although I had them all right. That's why I said "stuff it" and put it all in one big chunk, all coded.
That's when I found that I had used an @variable at a place where I should have used the plain variable. [tongue]

Well, to be honest, it does look a lot tidier again.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top