What is difference between #Html.RenderPartial("_Common.cshtml") and #Html.RenderPartial("_Common") - asp.net-mvc

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);
}

Related

What is the correct pattern to use when your PartialView wants to include scripts?

I have a bunch of partial views in my MVC 5 application.
They are used from a bunch of different pages. They have dependencies on certain script files.
Putting scripts in partial views appears to be a big no-no, so I've been putting the scripts in the parent pages.
I haven't been able to figure out how to keep my page rendering logic from being dispersed throughout several source files, and it is annoying to be forgetting references on different pages all the time when I add an existing partial view to them.
It seems like some sort of #include directive would be useful in razor partial views. Has someone bolted this on to MVC in a user contrib?
I like to use forloop htmlhelpers...
In global.asax...
Forloop.HtmlHelpers.ScriptContext.ScriptPathResolver = System.Web.Optimization.Scripts.Render;
In Layout.cshtml...
#Html.RenderScripts()
In Partial View...
#using (var ctx = Html.BeginScriptContext())
{
ctx.AddScriptFile("~/bundles/whatever");
ctx.AddScriptBlock(
#<script>
</script>
);
}
It'd be nice if this package handled css files also...usually a partial view is using some javascript that also requires some css.

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.

How to get current caller view page file in HTML helper extension method?

I create HTML helper extension to combine script into one file like the following code. It will combine both jQuery and jQuery UI file into common.js file at specified location. I use ASP.NET caching with file dependency to monitor all combined files. If some file is changed, the method will re-generate combined file.
#(Html.CombinedFiles
(
"~/Scripts/common.js",
"~/Scripts/jquery-1.4.4.js",
"~/Scripts/jquery-ui.js"
))
Everything works perfectly. However, it has only one problem. When developer modify view page that call this function for adding or removing file to be combined. ASP.NET caching is still valid. Method will not re-generate combined file because I don't add caller view page to file dependency list.
I want to know. Is it possible to get view page to call this extension method? It must support both ASPX and Razor view engine.
Thanks,
PS. Documentation about this function in my Higgs RIA framework for more understanding this method.
IView view = htmlHelper.ViewContext.View;
if (view is BuildManagerCompiledView)
{
string viewUrl = ((BuildManagerCompiledView)view).ViewPath;
// use viewUrl here
}

Razor: #Html.Partial() vs #RenderPage()

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" />

Action-View binding: magic!

Say there's HomeController with an Details-action. return View() will send data to the Detals.aspx in the Home folder. But who makes that binding? and what if I want it to go to Edit.aspx instead?
Background:
Alot of the code in Details.aspx and Edit.aspx is identical, save for one textbox. Maybe by MVC rigor, the view is not supposed to make that kind of decisions, but hey, there's got to be a limit.
You can make it go to Edit.aspx by specifying it as a parameter of the View() function.
return View("Edit");
As to who makes the actual binding happen, it's the View Engine. It receives the returned ViewResult and analyzes it to see which template file to load and display. When it gets the string "Edit", it runs a find routine, using the context of the controller, to search a number of directories for filenames that match the convention. It starts in the controller's View directory, and then searches the Shared directory.
If you want to Edit.aspx to be rendered you could return View("Edit");

Resources