Determine which view will be displayed in @RenderBody ()

In _Layout.cshtml it possible to determine which view will be displayed in @RenderBody() ?

+5
source share
6 answers

You can get a view (i.e. Index.cshtml) via ((RazorView)ViewContext.View).ViewPath

An example for your needs:

 <script type="text/javascript" src="~/Content/Scripts/@(Path.GetFileNameWithoutExtension(Server.MapPath(((RazorView)ViewContext.View).ViewPath))).js"></script> 

If you need your actual view (i.e. _Layout.cshtml), you can use VirtualPath instead.


Old answer While reading your comments, you want to add

 <script ...>...</script> 

depending on the view, but outside @RenderBody() ?

Then put

 @RenderSection("Scripts", required:false) 

and in your view define a section like

 @section Scripts { <script ...>...</script> } 

Thus, you do not need to maintain your _Layout.cshtml, as each view defines their own scripts.

Here is a simple explanation: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor

+11
source

What you can do is check out the Html.ViewContext.RouteData.Values . This is a dictionary with a controller, action and identifier (if necessary).

0
source

Read this article and it will solve your problem.

Edit

Renderbody

What is a RenderBody?

Page layouts display a portion of the content page that is not in the named section. [MSDN]

How does RenderBody (graphical representation) work? enter image description here

0
source

@RenderBody() displays a view controlled by a controller. therefore, if your controller is like that.

 public class HomeController : Controller { public ActionResult Index() // Renders File /Views/Home/Index.cshtml { return View(); } } 

Then the public ActionResult Index() Index.cshtml will display it in the / Views / Home folder.

You can add Index.cshtml or _Layout.cshtml to the view to display other views or partial images by adding @Html.Partial("_MyView") , as shown below.

  @Html.Partial("_LayoutHeaderHeader") @Html.Partial("_LayoutHeaderNavbar") 

Sometimes it’s easy to set up multiple pages of a layout for calling from different views.

If you want to call scripts for you View, you should always create _PartialView and place your scripts in a partial view and call this view at the bottom of your view, as shown below, @Html.Partial("_MyView") , and the scripts will be installed right.

Here is a good tutorial. http://www.codeproject.com/Articles/698246/ASP-NET-MVC-Special-Views-Partial-View-and-Layout

0
source

If you derived all your models from the base model, you can add a property to the base model, which returns the name of the controller that you can use with

this.RouteData.Values["controller"].ToString();

It would be even better if you had the BaseController class, because you could put it in the constructor and never have to touch it again.

Since you are returning a descendant of the base model to your index page with the controller name, now you can use some schema base on @Model.ControllerName . If your controller serves multiple views, the property can be updated to indicate a specific view name.

I do not think that you can get the name Partial inside the index if you are not using jquery, and by this time the page resources are already loaded.

Edit: Another trick is to create your own version of the Html.Partial () HtmlHelper class. So you have @ Html.MyPartial ("ViewName") , and inside this method, an internal function is called that generates Html.Partial and then enters your dependencies.

0
source

EDIT . I just read your comments about the problem and thought that the best way is to use the snipplet code provided by @Matt in another answer.

In your view, you can use the @section statement to tell you to load a script.

Layout template placeholder

 @RenderSection("scripts", required: false) 

Code view

 @section scripts { <script src="~/Scripts/custom-imgedit.js"></script> } 

In the above example, it is reported that custom-imgedit.js will be loaded at the location of the rendering section. Note. You can even use packages like @ Scripts.Render ("~ / bundles / myCustomScripts")

0
source

All Articles