Asked  7 Months ago    Answers:  5   Viewed   35 times

I have the following layout for my mvc project:

  • /Controllers
    • /Demo
    • /Demo/DemoArea1Controller
    • /Demo/DemoArea2Controller
    • etc...
  • /Views
    • /Demo
    • /Demo/DemoArea1/Index.aspx
    • /Demo/DemoArea2/Index.aspx

However, when I have this for DemoArea1Controller:

public class DemoArea1Controller : Controller
    public ActionResult Index()
        return View();

I get the "The view 'index' or its master could not be found" error, with the usual search locations.

How can I specify that controllers in the "Demo" namespace search in the "Demo" view subfolder?



You can easily extend the WebFormViewEngine to specify all the locations you want to look in:

public class CustomViewEngine : WebFormViewEngine
    public CustomViewEngine()
        var viewLocations =  new[] {  
            // etc

        this.PartialViewLocationFormats = viewLocations;
        this.ViewLocationFormats = viewLocations;

Make sure you remember to register the view engine by modifying the Application_Start method in your Global.asax.cs

protected void Application_Start()
    ViewEngines.Engines.Add(new CustomViewEngine());
Tuesday, June 1, 2021
answered 7 Months ago

You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

#IMPLIED means it is an optional attribute, or you could use #REQUIRED, etc.

More information is in DTD - Attributes.

Tuesday, June 1, 2021
answered 7 Months ago

There is no built in way to do this in MVC 3. You will have to write your helper that does this. Take a look at the LabelExtensions class to see how it is done.

Monday, August 9, 2021
answered 4 Months ago

It's not necessary to use an underscore, but it's a common convention for files which aren't meant to be served directly.

To solve this, you do have the option of returning a View or PartialView with the name of the view as a parameter.

return View("_List");


return PartialView("_List");

or inside another view

@{ Html.RenderPartial("_List"); }
Monday, September 13, 2021
answered 3 Months ago

You would use the first as a master view

@model MyTemplate.Models.LogOnModel
@ {
     Layout = "~/Views/LAYOUT OF THE MASTER PAGE" ;  
     Page.Title = "Log On";

check this link for more information


You can have a hiearchy of master views your post view can also have a layout.

What you can also do is inherit your models from the master views model


@model MyTemplate.Models.MasterModel
@ {
     Page.Title = "Log On";


@model MyTemplate.Models.PostModel
@ {
     Layout = "~/Views/Shared/master.cshtml" ;  


@model MyTemplate.Models.LogOnModel
@ {
     Layout = "~/Views/Shared/Post.cshtml" ;  
     Page.Title = "Log On";

Where LogOnModel inherits from PostModel and PostModel inherits from MasterModel

Sunday, November 21, 2021
David D.
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :