Can an ASP.NET MVC PartialView have a _Layout - asp.net

Can an ASP.NET MVC PartialView directly use a Layout?
MSDN
I have looked at MSDN for PartialViewResult and compared with ViewResult:
ViewResult has a property MasterName that PartialViewResult does not.
But.. I can still define the Layout property in the partial views Razor.
Background
I am currently remediating a large code base with a huge amount of partial views used to fill iframes. Ideally they would be converted to normal views (ideally we would not use iframes) but I was wondering about just adding a layout to these partials to take out the <head> element at least so that we have more control over the script versioning (everything is replicated in each partial view). I'm looking for a light-touch solution since much of the code is expected to be thrown away.

Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout.
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}

Yes.
You can use a layout within a layout:
Layout
View
Layout
PartialView
We have an example of this in our codebase.

Related

MVC4, two different masterpage

How can I use two different master page or layout in my web project with mvc4?
You create 2 different Layouts and then inside your view you can choose which layout to use:
#{
Layout = "~/Views/Shared/MyLayout.cshtml";
}
This can also be done inside the controller action rendering the view:
return View("Index", "MyLayout", myViewModel);
I don't claim to be an expert in MVC. As a matter of fact, I just started using it.
This is what I did. I copied Site.Master using Windows Explorer, renamed it, added it to Shared Views, edited it, and started using it. Worked for me.

ASP.Net MVC3: Hide item from _Layout file only in one view

In my layout file, I am displaying some text, and I want to hide this text only in one view. How do I tell what view is currently loaded from inside the view?
An easy way is to add some conditional logic to render in your _Layout as long as a viewbag bit is not set. If you don't want to render simply say so by defining the viewbag variable in the controller action and the layout won't render it.
There are more elegant solution involving attributes but this should get you going.
Don't forget the layout shouldn't know about specifically what view you're rendering thats a bleeding concerns. Viewbag helps by providing a communication and allowing decoupling of these two pieces.
You can set Id to that text container and in that specific view you can hide that Id by jquery
write this code in that view where you can to hide the text
$(function() { $("#id").hide(); }

Does ASP.NET MVC 3 Razor output buffer views?

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.

PartialViews in ASP.NET MVC4

I've noticed there are several ways to use Views and PartialViews:
RenderAction, RenderPartial, and "return PartialView"
RenderAction when placed inside HTML, will simply call an Action and Render the View returned (the View returned can be partial view or view?)
RenderPartial will simply retrieve the contents of a View without executing any Controller action
Finally, what's the difference between "Return View" and "Return PartialView"?
Thanks
return View() returns the view with a Layout enabled so you get full HTML page with <html> and <body> tags. return PartialView() on the other hand disables the Layout and you get only the HTML fragment contained in this view. Actually when working with the Razor view engine I prefer to talk about templates and not views and partial views. That's because a view is a template and a partial view is a template without a layout. But in both cases it's a Razor template.

How to organize partial view in ASP.NET MVC3?

I am trying to figure out how to organize my partial views in my project. Some people say to precede the name of a partial view with an _, but that makes for weirdly named actions in the controller if the view can be called directly.
Also, what should be done if the view can be a partial view in some cases and a regular view in other cases?
A common example for this is a search view that I embed on some pages to search for users in my app, but I also have a search page that loads the same view. I suppose I could create a second view for the search page that just embeds the partial view. Just wondering what other people are doing.
Honestly it's a matter of preference. You should do whatever works in your application with respect to avoiding code (or view) duplication etc.
The reason why we (I'm a dev on the team developing MVC) recommend preceding the partial view filename with an underscore is to more easily distinguish between full and partial views when looking at files in VS
I also use my partials with the underscore character as a prefix to easily distinguish between a view and a partial view when managing the files. As your project becomes bigger you may have a lot of files for a single controller, so this convention will help you a lot.
Besides, when you use a partial view you can call your views with an action using the following:
public ActionResult MyPartialAsAView()
{
// your code
return View("_myPartialView");
}
You have to remember that if you are using your partial as a View, you should assign the layout to it depending on the mode the partial is working (as a view or partial view), for example with a boolean property on your model class.

Resources