1

my problem is that I have a MVC4 project and I want to create some components that I want to add in some views using partial views. I will show you a picture of the folders logic that I want to create in my MVC project.

enter image description here

From this how can I add this partial views because when I try to acces them they are first looked in the main Views folder from my application, is there a way to make custum routes to my application?

@{ Html.RenderPartial("Components\\Chart\\View\\Index.cshtml");  }

This gives an error like this.

The partial view 'Conmponents\Chart\View\Index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Conmponents\Chart\View\Index.cshtml.aspx ~/Views/Home/Conmponents\Chart\View\Index.cshtml.ascx ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.aspx ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.ascx ~/Views/Home/Conmponents\Chart\View\Index.cshtml.cshtml ~/Views/Home/Conmponents\Chart\View\Index.cshtml.vbhtml ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.cshtml ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.vbhtml

Lain
  • 2,166
  • 4
  • 23
  • 47
Mihai
  • 518
  • 4
  • 12

1 Answers1

1

You can specify the path using ~ as the project directory.

Html.RenderPartial("~/Components/Chart/View/Index.cshtml",model)

Also this issue has nothing to do with the routing. Routing is about mapping incoming requests to your action methods. ASP.NET MVC tries to locate your views using controller and action names. If it's not there, it also check the Shared folder. You can override this behavior by specifying exact path, like in the example above.

Update:

Also you have to make some configurations to your View folder. Copying your Web.config inside the default Views folder or moving all view configurations to project's Web.config file should do the trick.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • The partial view '~/Components/Chart/View/Index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:... tried this... this is what i got. – Mihai Nov 03 '12 at 14:25
  • @Mihai I updated the answer, looks like we don't need the extension too.Is Components folder inside the main project folder? – Ufuk Hacıoğulları Nov 03 '12 at 14:27
  • Yes its in the main project, is a separate folder Components like the main folders for Controllers Views and Model... i could acces my view.. but it says now. The view at '~/Components/Chart/View/Chart.cshtml' must derive from WebViewPage, or WebViewPage. – Mihai Nov 03 '12 at 14:42
  • i copied webconfig file in my view folder and now i have a problem with the virtual directories. It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. – Mihai Nov 03 '12 at 14:59
  • @Mihai You may want to take a look at this [question](http://stackoverflow.com/q/2355947/205859) – Ufuk Hacıoğulları Nov 03 '12 at 15:08
  • I read that article but they say that for any folder that has a webconfig i must add that in the IIS server configuration... and that with the development IIS is quite tricky for me ... – Mihai Nov 03 '12 at 15:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19017/discussion-between-ufuk-haciogullari-and-mihai) – Ufuk Hacıoğulları Nov 03 '12 at 15:16