How to set Razor Layout file just specifying the name? - asp.net-mvc

First a little context. When you call Html.RenderPartial you send the View name, that view will be searched at locations specified by RazorViewEngine.PartialViewLocationFormats:
Html.RenderPartial("Post", item);
When you set the Layout property at Razor page, you canĀ“t just say the name, you need to specify the path. How can I just specify the name?
//Layout = "_Layout.cshtml";
Layout = "_Layout"; //Dont work
I need this because I overrided the RazorViewEngine.MasterLocationFormats.
Currently I am specifying the Master at controller:
return View("Index", "_Layout", model);
This works, but I prefer to do this at View.

There is no direct way to do it,
But we can write an HtmlExtension like "RenderPartial()" which will give complete layout path at runtime.
public static class HtmlExtensions
{
public static string ReadLayoutPath<T>(this HtmlHelper<T> html,string layoutName)
{
string[] layoutLocationFormats = new string[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
foreach (var item in layoutLocationFormats)
{
var controllerName= html.ViewContext.RouteData.Values["Controller"].ToString();
var resolveLayoutUrl = string.Format(item, layoutName, controllerName);
string fullLayoutPath = HostingEnvironment.IsHosted ? HostingEnvironment.MapPath(resolveLayoutUrl) : System.IO.Path.GetFullPath(resolveLayoutUrl);
if (File.Exists(fullLayoutPath))
return resolveLayoutUrl;
}
throw new Exception("Page not found.");
}
}
In the view we can use it as,
#{
Layout = Html.ReadLayoutPath("_Layout");
}

Can I ask why you are doing this or more specifically why are you returning a layout page from a controller? You are missing the point of master pages it seems.
You can't specify just the "name", you need to specify the path of the layout view so that it can in turn be applied to the view are rendering.
Layout = "~/SomeCustomLocation/SomeFolder/_Layout.cshtml"

Related

What happens when you return View when should be PartialView?

In a View I am calling an action that returns a View
View:
Html.RenderAction("Read", "Stats", new { Module = statsModel.Module, Name = statsModel.Name });
Controller:
public ActionResult Read(Module module, string name, bool showStatsItems = true)
{
eRPortalEntities db = new eRPortalEntities();
StatsPanelService service = new StatsPanelService(db, UserID);
StatsPanelViewModel spv = service.Read(module, name);
spv.ShowStatsItems = showStatsItems;
return View("StatsPanel", spv);
}
This unfortunately causes some of my Bootstrap functionality to break. Such as dropdowns and modals not toggling.
If instead I have the controller return a PartialView, everything works as expected
return PartialView("StatsPanel", spv);
I'm not looking for a specific reason why my bootstrap stopped working but more of an explanation of... Why would this cause issues in general?
Unless you explicitly specify the Layout to be null, When you do return View("StatsPanel"), Razor view engine will render the view content inside the Layout (_Layout.cshtml) similar to how you render a page normallly. That means, it will include all those scripts & Css in the head section again. That could be the reason it is messing up your markup.
Using PartialView() method seems appropriate in your use case. If you still want to use the View() method, you can explicitly define layout as null in your StatsPanel.cshtml view like this
#{ Layout = null; }

How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side.
For simplicity, let's say all the logic for each of these sites is wrapped neatly into 2 distinct controllers.
PublicController
StaffController
And that they each have a corresponding Layout for all the View under each.
_PublicLayout.cshtml
_StaffLayout.cshtml
How do I use the _ViewStart.cshtml file to specify that all Views / Actions under "Public" use the PublicLayout and everything under "Staff" uses the StaffLayout?
You could put a _ViewStart.cshtml file inside the /Views/Public folder which would override the default one in the /Views folder and specify the desired layout:
#{
Layout = "~/Views/Shared/_PublicLayout.cshtml";
}
By analogy you could put another _ViewStart.cshtml file inside the /Views/Staff folder with:
#{
Layout = "~/Views/Shared/_StaffLayout.cshtml";
}
You could also specify which layout should be used when returning a view inside a controller action but that's per action:
return View("Index", "~/Views/Shared/_StaffLayout.cshtml", someViewModel);
Yet another possibility is a custom action filter which would override the layout. As you can see many possibilities to achieve this. Up to you to choose which one fits best in your scenario.
UPDATE:
As requested in the comments section here's an example of an action filter which would choose a master page:
public class LayoutInjecterAttribute : ActionFilterAttribute
{
private readonly string _masterName;
public LayoutInjecterAttribute(string masterName)
{
_masterName = masterName;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = _masterName;
}
}
}
and then decorate a controller or an action with this custom attribute specifying the layout you want:
[LayoutInjecter("_PublicLayout")]
public ActionResult Index()
{
return View();
}
One more method is to Define the Layout inside the View:
#{
Layout = "~/Views/Shared/_MyAdminLayout.cshtml";
}
More Ways to do, can be found here, hope this helps someone.
This method is the simplest way for beginners to control Layouts rendering in your ASP.NET MVC application. We can identify the controller and render the Layouts as par controller, to do this we can write our code in _ViewStart file in the root directory of the Views folder. Following is an example shows how it can be done.
#{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
string cLayout = "";
if (controller == "Webmaster")
cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";
else
cLayout = "~/Views/Shared/_Layout.cshtml";
Layout = cLayout;
}

Determine the model of a partial view from the controller within MVC

My current problem is that I have a partial view that I want to determine what model is being used by it.
I have had to deal with a few strange scenarios for my project so I will try to outline it here, maybe someone can offer a better way to do this.
I am designing something like the Google iGoogle page. A main page with multiple widgets that are able to move around or be configured as needed. The current system loads the actual widget's data asynchronously view a POST to a controller within my application. That controller will either render a partial view to HTML that can be returned (and then loaded into the page view JQUERY) or just straight HTML/JavaScript that is stored in a database.
This was working fine for me, I had a model for the widgets that holds a dictionary of options that are described via the database, and then used by the partial view. The problem came when I wanted to pass data to a partial view. The best solution I could come up with was having the controller determine which model the partial view in question uses, have some function that will fill the model, and then pass it, along with the partial view, to the function that will render it to HTML within the controller.
I realize this is an odd scenario for MVC (the layers are blending...) and any advice on fundamental design, or implementation of this would be greatly appreciated.
I am currently using MVC3/Razor. Feel free to ask any other questions.
I prototyped a possible solution to this, because it seemed like a fun problem. I hope it's useful to you.
Models
First, the models. I decided to create two 'widgets', one for news, and one for a clock.
public class NewsModel
{
public string[] Headlines { get; set; }
public NewsModel(params string[] headlines)
{
Headlines = headlines;
}
}
public class ClockModel
{
public DateTime Now { get; set; }
public ClockModel(DateTime now)
{
Now = now;
}
}
Controller
My controller doesn't know anything about the views. What it does is returns a single model, but that model has the ability to dynamically fetch the right model as required by the view.
public ActionResult Show(string widgetName)
{
var selector = new ModelSelector();
selector.WhenRendering<ClockModel>(() => new ClockModel(DateTime.Now));
selector.WhenRendering<NewsModel>(() => new NewsModel("Headline 1", "Headline 2", "Headline 3"));
return PartialView(widgetName, selector);
}
Delegates are used so that the correct model is only created/fetched if it is actually used.
ModelSelector
The ModelSelector that the controller uses is pretty simple - it just keeps a bag of delegates to create each model type:
public class ModelSelector
{
private readonly Dictionary<Type, Func<object>> modelLookup = new Dictionary<Type, Func<object>>();
public void WhenRendering<T>(Func<object> getter)
{
modelLookup.Add(typeof(T), getter);
}
public object GetModel(Type modelType)
{
if (!modelLookup.ContainsKey(modelType))
{
throw new KeyNotFoundException(string.Format("A provider for the model type '{0}' was not provided", modelType.FullName));
}
return modelLookup[modelType]();
}
}
The Views - Simple solution
Now, the easiest way to implement a view would be:
#model MvcApplication2.ModelSelector
#using MvcApplication2.Models
#{
var clock = (ClockModel) Model.GetModel(typeof (ClockModel));
}
<h2>The time is: #clock.Now</h2>
You could end here and use this approach.
The Views - Better solution
That's pretty ugly. I wanted my views to look like this:
#model MvcApplication2.Models.ClockModel
<h2>Clock</h2>
#Model.Now
And
#model MvcApplication2.Models.NewsModel
<h2>News Widget</h2>
#foreach (var headline in Model.Headlines)
{
<h3>#headline</h3>
}
To make this work, I had to create a custom view engine.
Custom view engine
When a Razor view is compiled, it inherits a ViewPage<T>, where T is the #model. So we can use reflection to figure out what type the view wanted, and select it.
public class ModelSelectorEnabledRazorViewEngine : RazorViewEngine
{
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var result = base.CreateView(controllerContext, viewPath, masterPath);
if (result == null)
return null;
return new CustomRazorView((RazorView) result);
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var result = base.CreatePartialView(controllerContext, partialPath);
if (result == null)
return null;
return new CustomRazorView((RazorView)result);
}
public class CustomRazorView : IView
{
private readonly RazorView view;
public CustomRazorView(RazorView view)
{
this.view = view;
}
public void Render(ViewContext viewContext, TextWriter writer)
{
var modelSelector = viewContext.ViewData.Model as ModelSelector;
if (modelSelector == null)
{
// This is not a widget, so fall back to stock-standard MVC/Razor rendering
view.Render(viewContext, writer);
return;
}
// We need to work out what #model is on the view, so that we can pass the correct model to it.
// We can do this by using reflection over the compiled views, since Razor views implement a
// ViewPage<T>, where T is the #model value.
var compiledViewType = BuildManager.GetCompiledType(view.ViewPath);
var baseType = compiledViewType.BaseType;
if (baseType == null || !baseType.IsGenericType)
{
throw new Exception(string.Format("When the view '{0}' was compiled, the resulting type was '{1}', with base type '{2}'. I expected a base type with a single generic argument; I don't know how to handle this type.", view.ViewPath, compiledViewType, baseType));
}
// This will be the value of #model
var modelType = baseType.GetGenericArguments()[0];
if (modelType == typeof(object))
{
// When no #model is set, the result is a ViewPage<object>
throw new Exception(string.Format("The view '{0}' needs to include the #model directive to specify the model type. Did you forget to include an #model line?", view.ViewPath));
}
var model = modelSelector.GetModel(modelType);
// Switch the current model from the ModelSelector to the value of #model
viewContext.ViewData.Model = model;
view.Render(viewContext, writer);
}
}
}
The view engine is registered by putting this in Global.asax.cs:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ModelSelectorEnabledRazorViewEngine());
Rendering
My home view includes the following lines to test it all out:
#Html.Action("Show", "Widget", new { widgetName = "Clock" })
#Html.Action("Show", "Widget", new { widgetName = "News" })
One option would be to extend the idea of partial requests in your application. Steve Sanderson has a fantastic example of this, although the post relates to MVC 1 & 2. I think it would still help in you v3, but I haven't investigated v3 to see if the MVC team implemented their own version. In your asynch scenario, you'll need to toy with the implementation a bit, perhaps change the PartialRequest definition to accept different information as needed, but I think this might be a good start. The net result would be better isolation of concerns, allowing individual controllers to manage a particular type of partial, and in turn be better aware of the model Type you want to work with.
I'm not 100% sure that this is what you'd be looking for, but the [ChildActionOnly] attribute can be added to a method within your controller. That requires that the method can only be called from a partial view. Then you can set up your partial view for that method that basically resembles one of your widgets. Check out the MVC Music Store example here:
http://www.asp.net/mvc/tutorials/mvc-music-store-part-10
What about a dynamic view model? Layouts in MVC3 use them, and maybe you can use something similar for your purposes:
Dynamic in C# 4.0: Introducing the ExpandoObject
Fun With Method Missing and C# 4
Dynamic View Page, MVC without a View Model
I blogged about doing exactly this. Please see http://blogs.planetcloud.co.uk/mygreatdiscovery/?tag=/widget
Essentially I built out a similar widget system. The posts also cover how to handle configuration of those widgets. This makes use of the dynamic support in Mvc3 so that any model object can be passed to the view, from a single controller action.
By default all widgets have a collection of KVP properties (I believe this is what the OP has). So for a simple widget we get access to those properties from within the view. I used for a widget that displayed some html (where the html was stored in one of those properties).
However, for more complex widgets we implement IWidgetWithDisplayModel. This tells us that before we pass the loaded widget back to the view, we need to "build" our display model.
Here's the controller action that does that. Check the posts for full details.
[HttpGet]
public ActionResult Get(string name)
{
var widget = widgetService.GetWidgetBySystemName(name, true);
if (widget == null)
return Content(string.Format("Widget [{0}] not found!", name));
if (!this.ViewExists(widget.WidgetName))
return Content(string.Format("A template for widget [{0}] was not found.", widget.WidgetName));
if (widget is IWidgetWithDisplayModel) {
(widget as IWidgetWithDisplayModel).CreateDisplayModel();
}
return PartialView(widget.WidgetName, widget);
}

MasterLocationFormats in WebFormViewEngine not used?

I tried to make the ViewEngine use an additional path using:
base.MasterLocationFormats = new string[] {
"~/Views/AddedMaster.Master"
};
in the constructor of the ViewEngine. It works well for aspx and ascx(PartialViewLocationFormats, ViewLocationFormats).
I still have to supply the MasterPage in web.config or in the page declaration. But if I do, then this declaration is used, not the one in the ViewEngine.
If I use am empty MasterLocationFormats, no error is thrown. Is this not implemeted in RC1?
EDIT:
using:
return View("Index", "AddedMaster");
instead of
return View("Index");
in the Controller worked.
Your example isn't really complete, but I am going to guess that your block of code exists at the class level and not inside of a constructor method. The problem with that is that the base class (WebFormViewEngine) initializes the "location format" properties in a constructor, hence overriding your declaration;
public CustomViewEngine()
{
MasterLocationFormats = new string[] {
"~/Views/AddedMaster.Master"
};
}
If you want the hard-coded master to only kick in as a sort of last effort default, you can do something like this:
public CustomViewEngine()
{
MasterLocationFormats = new List<string>(MasterLocationFormats) {
"~/Views/AddedMaster.Master"
}.ToArray();
}

Change lookup rule for views

I have an application that gets rolled out in multiple countries. There will be a setting in the web.config file, that defines the country.
The country will not be in the URL.
Some of the the views change depending on the country.
My first attempt is to use a folder inside the views folder that contains views, if they differ from the default view:
Default
/questions/ask.aspx
Spain
/questions/ESP/ask.aspx
If there is no view in the country-folder the default view is used. Is there a way to extend the ViewEngine to lookup views in the country folder first?
EDIT:
This is a poc only. To see a full implementation have a look at
http://pietschsoft.com/?tag=/mvc
private static string[] LocalViewFormats =
new string[] {
"~/Views/{1}/ESP/{0}.aspx",
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx"
};
public LocalizationWebFormViewEngine()
{
ViewLocationFormats = LocalViewFormats;
}
public class MyViewEngine : WebFormViewEngine
{
private static string[] LocalViewFormats = new[] { "~/Views/ESP/{0}.aspx",
"~/Views/ESP/{0}.ascx" };
public MyViewEngine()
{
ViewLocationFormats = LocalViewFormats.Union(ViewLocationFormats).ToArray();
}
}
Obviously, you don't want to hardcode the location, but this should give you the general idea.

Resources