Razor: #Html.Partial() vs #RenderPage() - asp.net-mvc

What is the appropriate way of rendering a child template?
And what's the difference? Both seem to work for me.
And why does #Html.RenderPartial() no longer work?

Html.Partial("MyView")
Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).
Html.RenderPartial("MyView")
Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, #Html.RenderPartial("MyView") won't work. You have to wrap the call in a code block instead: #{Html.RenderPartial("MyView");}.
RenderPage("MyView.cshtml")
Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). You can supply any model you like to the view by including it as a second parameter
RenderPage("MyView.cshtml", MyModel)

I prefer
#RenderPage("_LayoutHeader.cshtml")
Over
#{ Html.RenderPartial("_LayoutHeader"); }
Only because the syntax is easier and it is more readable. Other than that there doesn't seem to be any differences functionality wise.
EDIT: One advantage of RenderPartial is you don't have to specify the entire path or file extension it will search the common places automatically.

The RenderPartial method doesn’t return HTML markup like most other helper methods. Instead, it writes
content directly to the response stream, which is why we must call it like a complete line of C#, using a semicolon.
This is slightly more efficient than buffering the rendered HTML from the partial view, since it will be written to the
response stream anyway. If you prefer a more consistent syntax, you can use the Html.Partial method, which
does exactly the same as the RenderPartial method, but returns an HTML fragment and can be used as
#Html.Partial("Product", p).

We can also pass model using partial views. #Html.Partial("MyView","MyModel");

#RenderPages()
The above does not work in ASP.NET MVC. It only works in WebPages.
#Html.Partial("_Footer")
You will need to use the above in ASP.NET MVC.

For ASP.NET Core 7. In the Shared folder make partial file then user this following code
<partial name="_NavBar" />

Related

What is difference between #Html.RenderPartial("_Common.cshtml") and #Html.RenderPartial("_Common")

I am facing one problem in MVC4. If I am not providing .cshtml while calling RenderPartial then it is not calling Partial View.
For Example
#Html.RenderPartial("_Common.cshtml") //it is working
#Html.RenderPartial("_Common") //it is not working
My question is why it is not working?
As Zabavsky mentions, you may have an incorrect filename on your partial view (double extension). Just did that yesterday on a project, so easy to do, but I think you should be using Partial and not RenderPartial
Just to clarify which options should work:
1) The extension of the view file is required if you supply a path.
2) If you do not supply a path, do not supply the extension.
The examples below assume cshtml files.
Use RenderPartial in a code block:
// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
Html.RenderPartial("DefinitionDetails");
// This looks in specified path and requires the extension
Html.RenderPartial("~/Views/Definition/DefinitionDetails.cshtml");
Use Partial for inline Razor syntax:
// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
#Html.Partial("DefinitionDetails")
// This looks in specified path and requires the extension
#Html.Partial("~/Views/Definition/DefinitionDetails.cshtml")
Note: Apparently RenderPartial is slightly faster than Partial, but I also expect fully pathed names will be a faster than letting MVC search for the file.
If you are producing partials in a loop (i.e. from a collection in your view model), it is likely you will need to pass through specific viewmodels:
e.g.
#foreach (var group in orderedGroups)
{
Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", group);
}

Define something like for in ASP.NET MVC 3 to pass real html

How can define a custom block like #for in Razor view? I read about #helper here and here also there is a similar question here but #helper method just define html tags at first then when you want to use in views just can pass some parameters, in other hand #for block is that I want, in views you can pass Html (with intelligence support), Does any one have any idea about this? I also think about modify razor source code! (if possible) Is there any better way?
You may take a look at Razor Templated Delegates.

How do I encapsulate calls to ViewBag or ViewData?

I want to be able to able to register script blocks in the ViewData or ViewBag and then unload them on my layout page at the correct position.
I tried writing a #function {} in my _Layout but this cannot be called from my Views.
So how do I write a class that allows me to do something similar to
#Something.registerscript("myscript.js")
And then on the Layout page
#Something.RenderScripts()
I saw an implementation using the singleton pattern here...
Add CSS or JavaScript files to layout head from views or partial views
But im afraid that will cause problems as this should be dynamic not static!
Is this for a generic solution or do you just want to include scripts files from within your views? For the latter, you can always create a section called "Head" or whatever which is created within the <head>-element.
For example:
_layout.cshtml
<html>
<head>
#RenderSection("Head", false)
</head>
...
</html>
View.cshtml
#section Head
{
<script type="text/javascript">...</script>
}
I wouldn't propose this as an answer, since there is the possiblity that you do want to actually do more than this (as far as I know, this doesn't work with EditorTemplates etc.). But in case you were thinking to complicated, this works very easy.
Why would you use ViewBag for this ? If you want to create something that has request-wide scope, use HttpContext.Current.Items - in your case implement one storing helper method, and one "render everything stored" method.
Viewbag or it's close relative viewdata I'd say are the best collections to use for this, as your context appears to be within the view. Where would you register the script & where would you render it?
You could, I think, write helper extensions to wrap up the register & render side of things. I believe helper methods can access viewdata. You'd get the added bonus of the abstraction away from the actual store you used, as you'd only reference it from these helper methods.

Is it possible to simplify the meta data at the top of a MVC Razor file?

This doesn't really look so clean. Can I simplify this by putting all in just one curly brace?
#model Test.WebUx.Areas.Administration.ViewModels
#using Test.Shared.ExtensionMethods;
#{ Layout = "~/Areas/Administration/Views/_locs.cshtml"; }
The #model directive is required and has to be on it's own I believe.
You can however move "Text.Shared.ExtensionMethods" into the namespaces element in the web.config file in your Views folder and it will be imported on all of your views. This is especially useful if these functions are used in multiple views.
I don't think you can do so. It's just like an HTML tagging system, but without any closing tag. To use Razor view engine, one uses # to start the Razor code and fetch values that are being passed by controller.

Asp.Net WriteSubsitution vs PartialView - the right way

I have a partial view that should not be cached in a output cached MVC view.
Usually you write non-cached content by using Response.WriteSubstitution.
The problem is that WriteSubstitution takes as a parameter a HttpResponseSubstitutionCallback callback which looks like this:
public delegate string HttpResponseSubstitutionCallback(System.Web.HttpContext context)
This is where things get complicated since there is no easy/fun way to generate the html on the fly.
You have to do a hack like this.
So the question is: Is there an easier way to make a partial view not cached ?
See Phil Haack's article on donut caching in MVC. Phil takes advantage of the existing API to create a new HtmlHelper method to provide a callback that can render the non-cached code. His supplies an anonymous method to the helper to specify the callback. To get this to work unchanged, you'll still need to have a method that renders a partial view to a string, though I think it would be easier to do in an HtmlHelper -- just look at the source for RenderPartial and RenderPartialInternal and see what changes would be needed to write it to a MemoryStream instead of the Response -- I believe it would be the same code except you'd supply your stream instead of the Response output stream, then convert your stream to a string.
It might look like this:
<%= Html.Substitute( cb => Html.RenderToString( "Partial" ) ) %>
Phil indicates that it might make it in to ASP.NET MVC 1.0, but I think it's only available in the MvcFutures assembly.
In MVC2 we can use Html.Action to easily obtain the substituted html. Yeeeeiii
But Response.WriteSubstitotion is not working anymore. Aaaahhhh

Resources