Does ASP.NET MVC 3 Razor output buffer views? - asp.net

In ASP.NET MVC 3 Razor, you can specify the page title with:
#{
ViewBag.Title = "Title";
}
Now, suppose we have a layout page with:
<title>#ViewBag.Title | Website</title>
When ASP goes to render a page, it will need to output some of the layout page HTML, then the view HTML, then the rest of the layout page HTML.
In order to output the first half of the layout page HTML, ASP.NET will need to know the value given to ViewBag.title in the view. Thus, ASP.NET needs to parse the Razor code in the view. However, ASP.NET can't output the view's HTML code just yet because it is still outputting the layout page's HTML code. So does ASP.NET store the HTML output of the view in a buffer? That seems like a bad practice, but I can't think of any other way to efficiently get the view's title into the layout page output.

When ASP goes to render a page, it will need to output some of the layout page HTML, then the view HTML, then the rest of the layout page HTML.
That's how web forms are rendered. However due to the issue you describe (along with a few others) mvc renders inside out.
So the inner view is rendered first to a temporary buffer. Then the layout page. This rendering continues until the outer most layout page is reached when the buffer is then wrote to the response stream and flushed.
This doesn't cause any issues 90% of the time (guestimate) however this will cause headaches if you ever need to flush the response early.
FYI the buffer for the views can be accessed with:
HtmlHelper.ViewContext.Writer
So to answer your question, yes - it does buffer views.

Related

What's the best way of structuring hiearchy of views in ASP.NET MVC?

I'm currently converting a ASP.NET webforms solution to MVC.
In webforms we besides the site.master we have master pages where we keep the common code and head imports for each section. So a normal page inheritance goes like so:
Site.master -> section.Master -> page.aspx
In MVC I'm unclear as to where to place the code common to a section. Is splitting the section content into partial views the only solution? So it becomes?
Layout.cshtml -> page.cshtml -> any-number-of-partial-views
To give an example, if I need a single CSS file to be shared across all 10 section pages, I can put it in a partial view and render it on each page but doesn't feel incredibly efficient. Or maybe I just need to get my head around to this new way of working.
You can use sections in MVC as well.
In your _Layout.cshtml you might have something like this:
#RenderSection("testSection",required:false)
And in your child view you can specify what HTML should be rendered in this section or you can leave it all together because required has been set to false:
#section testSection{
<h1>Test</h1>
}
Partial view is your big friend if you would like to reuse the code several places.
You can put partial view inside Views > ControllerNameAsFolderName > view pages (put here) to use only in same controller views or even inside Shared folder to use globally.
section is another option, this is like UserControl (in web forms). create section reference #RenderSection("sectionName", required:false) on Layout page and use it on view pages like
#section sectionName{
<div>content goes here</div>
}
Assume you want to put something (like meta infos) inside <head> from view pages, in this case you can create a section in _Layout.cshtml page and then use that section on view pages.
This way you can place your code at certain location in DOM structure from any page. section has a great feature that allow you to mark it as required:false/true.

In aspx with mvc 4, how to modify layout pages slightly on opening of a new page

When an htmlActionLink is clicked, a new page (say About) is opened with layout and the main body is rendered from the corresponding view. I want to change the style of the about link to show that this link is active. I tried a lot using jquery. It changes momentarily, but it comes back to its original style. I have not tried it through controller. I've used jquery in the layout.cshtml and about.cshtml page.
Look in to MvcSiteMap. You can use the MVC helpers to create a navigation menu that changes as you visit pages. Also, keeping it server side over using jQuery is going to be the best bet.

how to Print asp.net user controls in separate pages?

I have around 10 usercontrols ( each user control has textbox ,gridview) to be displayed on a single page which needs to be printed using cutepdf.
The issue is the user controls breaks into seperate page (like textbox brteaking into pages). Is there a way to print a asp.net page selectively into separate page ?
The CSS properties page-break-before, page-break-inside, and page-break-after control where page breaks occur when a web page is printed from a browser.
Assuming CutePDF accurately represents the pages as they would have been printed, these should help you achieve page breaking the way you want and avoid breaking pages mid-control.
(Of course, if something, like a table, is too long for one page, a page break is inevitable. But these CSS properties will at least give you some control over how they occur.)

css for a content page is converting to "QuirkMode" in asp.net

hey guys, i have a page "shopingcart.aspx" in one of the contentplaceholder of master page, now when i load this page css is render just fine, but when i go to this page by postback e.g "shopingcart.aspx?itemid=1" then the css is rendering like quirkmode. All the content of the table are showin' bigger, any one out there faced this kind a problem ? any idea guys please help me out here..
Replace Response.write() whereever you are using it with Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "your script").
Response.Write has no place in web forms based ASP.NET. Anything that you response.write will get injected in the response before the rest of the page. Look at View Source on the rendered page and you will see that your <script> tag appears before the <html> tag (which should be the first one).

ASP.NET MVC: Basic form question

A friend has asked me to help him with a project that's MVC. This is my first experience with MVC. I'm trying to build the MVC components for a form for him.
A page has a modal popup which uses a JavaScript to POST or GET and receive HTML backā€”that it displays inside the popup modal.
I need to create an MVC form that has validation to display inside aforementioned popup. This popped-up form will be used elsewhere on the site, so needs to be modular.
Should I create an ActionResult in my Route's Controller that returns an View (.aspx) containing my form? Should I instead create a Partial View (.ascx) that has the form, then use that Partial View in a View for my Route so other parts of the site--other Routes--can do the same?
I'm stuck at that basic understanding. From there, I don't even know what to do about the validation (was told the same validation will be used on a nearly identical form) and how a ViewModel fits into this like is used elsewhere on the site.
I've been reading a lot and looking at a lot of examples but I'm still confused.
You've got two separate questions here really. To deal with whether you should use a partial view or a regular view, just think of it this way. Does the HTML content in question represent a full page, or just a piece of a page that will be reused inside of other pages?
If it's the former, then use a full View. If it's the latter, then put it in a partial view. It sounds to me like you just need a full View. In either case, it's easy to change to the other if it turns out it's not meeting your needs.
As to validation, take a look at xVal if you're using ASP.Net MVC 1. It allows for easy validation using attributes on your models.
When you make your post from the modal popup do the validation then in the actionmethod you created specifically for that popup. If you want client side validation write up the js to do it.
As for the modal markup and what not just create a partial view for that, shouldn't be a big deal.

Resources