They should all be under the home directory bacause that's were MVC expects them to be.
Of course you could write your own Module to pull View from a different location but that's really more work than it's worth. That's why most of us just leave the Views under the Views/ControllerName directory.
VS knows nothing about your changes to the view engine. it just knows the MVC defaults. if you change the search logic for views, don't expect VS to support the changes. you will lose this VS feature.
wkl1999143
Member
3 Points
33 Posts
controller can not find view
Jan 06, 2013 02:43 PM|LINK
if i would like to move the cshtml file from views/home/xx.cshtml to views/home/example1/xx.cshtml
when i click go to view in the homecontroller, the homeController can not find the view.
can i solve this problem?
eric2820
Contributor
2777 Points
1161 Posts
Re: controller can not find view
Jan 06, 2013 02:52 PM|LINK
That's because in MVC the folders are searched by MVC in this order:
Views/Controller/View.cshtml
When you move a view that breaks the connection between the View and the Controller.
Move the view back to where it came from and the Controller will see it again.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
wkl1999143
Member
3 Points
33 Posts
Re: controller can not find view
Jan 06, 2013 02:57 PM|LINK
if i have too many cshtml file as below:
views/home/example1/a.cshtml
views/home/example1/b.cshtml
views/home/example1/c.cshtml
views/home/example2/a.cshtml
views/home/example2/b.cshtml
views/home/example2/c.cshtml
....
can i group this file in different folder?
eric2820
Contributor
2777 Points
1161 Posts
Re: controller can not find view
Jan 06, 2013 03:06 PM|LINK
Not that I'm aware of.
They should all be under the home directory bacause that's were MVC expects them to be.
Of course you could write your own Module to pull View from a different location but that's really more work than it's worth. That's why most of us just leave the Views under the Views/ControllerName directory.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
CPrakash82
All-Star
18270 Points
2839 Posts
Re: controller can not find view
Jan 06, 2013 03:35 PM|LINK
An alternative could be to return the view along with the view name so that MVC will map it.
return View("~/views/home/example2/c.cshtml", model);
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: controller can not find view
Jan 06, 2013 06:00 PM|LINK
Yo[u can change where the view engine looks for views by configuring it:
protected void Application_Start() { RazorViewEngine rve = new RazorViewEngine(); rve.PartialViewLocationFormats = rve.ViewLocationFormats = rve.MasterLocationFormats = new string[] { "~/Assets/Views/{1}/{0}.cshtml", "~/Assets/Views/Shared/{1}/{0}.cshtml"}; rve.AreaMasterLocationFormats = rve.AreaPartialViewLocationFormats = rve.AreaViewLocationFormats = new string[] { "~/Assets/Areas/{2}/Views/{1}/{0}.cshtml", "~/Assets/Areas/{2}/Views/Shared/{1}/{0}.cshtml"}; ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(rve); }DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
wkl1999143
Member
3 Points
33 Posts
Re: controller can not find view
Jan 07, 2013 01:17 AM|LINK
after i add RazorViewEngine rve = new RazorViewEngine();
when i click go to view in the controller, vs can not search the view. "unable to find the matching view"
BrockAllen
All-Star
27522 Points
4901 Posts
MVP
Re: controller can not find view
Jan 07, 2013 01:42 AM|LINK
This doesn't tell me much. Does your code in App_Start look exactly the same?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
wkl1999143
Member
3 Points
33 Posts
Re: controller can not find view
Jan 07, 2013 01:53 AM|LINK
yes,
it can search the view in the browser, but it can't find the view in the homecontroller.
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: controller can not find view
Jan 07, 2013 04:36 AM|LINK
VS knows nothing about your changes to the view engine. it just knows the MVC defaults. if you change the search logic for views, don't expect VS to support the changes. you will lose this VS feature.
note: MVC stresses convention over configuration.