Reputation: 539
I use AspNet MVC 4. I use partial views on my project. I put partial views in shared/partial folder. And I call relating partial page in a view.
For example, I add _category.cshtml partial view to Views/Categories/Index.cshtml like below.
@Html.Partial("~/Areas/Admin/Views/Shared/partial_leftmenu/_category.cshtml")
My question is that, I have to write all path "~/Areas/Admin/Views/Shared/partial_leftmenu/...." for calling any partial view. I try to find a solution for writing this path shortly.
For example, I will define "~/Areas/Admin/Views/Shared/partial_leftmenu" path as a variable, and I write this variable instead of "~/Areas/Admin/Views/Shared/partial_leftmenu" path. I try this this, but it could not work.(like below)
@Html.Partial(partial_path+"/_category.cshtml")
How can I solve this? Thanks.
Upvotes: 2
Views: 6706
Reputation: 886
You can change where MVC searches to include custom folders. See here.
Upvotes: 1
Reputation: 13233
I believe MVC automatically searches through all files and folders in Views/Shared so I think all you need is:
@Html.Partial("_category")
Beyond that if you're in a specific area then it searches both main/root views folder and the current areas views folder. If you're trying to access partial from one area while you're currently in another then you're doing something wrong.
Upvotes: 2