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?

 Answers

94

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[] {  
            "~/Views/{1}/{0}.aspx",  
            "~/Views/{1}/{0}.ascx",  
            "~/Views/Shared/{0}.aspx",  
            "~/Views/Shared/{0}.ascx",  
            "~/AnotherPath/Views/{0}.ascx"
            // 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.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
}
Tuesday, June 1, 2021
 
michele
answered 7 Months ago
33

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
  <!ATTLIST tag myAttri CDATA #IMPLIED>
]>

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

More information is in DTD - Attributes.

Tuesday, June 1, 2021
 
HexaGridBrain
answered 7 Months ago
99

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
 
konstantin
answered 4 Months ago
84

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

or

return PartialView("_List");

or inside another view

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

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

http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

EDIT:


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

"~/Views/Shared/Master.cshtml"

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

"~/Views/Shared/Post.cshtml"

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

"~/Views/Post/default.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 :  
Share