How to use the Html.Partial () method to render a partial view with an explicit path

Everyone, I use MVC 3 (Razor). I have the following problem:

I have general content divided into a partial view. But instead of putting it in the default location ( views/shared or views/controller-name ), I need to put it in another place ( views/shared/new-folder or view/controller-name/new-folder ).

I tried this: @Html.Partial("views/shared/new-folder/partial-view-name") or even @Html.Partial("views/shared/new-folder/partial-view-name.cshtml") , but it seems that MVC3 considers this parameter only as a view name and completely ignores any path information.

Maybe I did something wrong, can someone help me? :) Thanks a lot!

+6
source share
2 answers

You need to reference the virtual path of the application (pay attention to ~\ at the beginning of the path):

 @Html.Partial("~\\views\\shared\\new-folder\\partial-view-name.cshtml") 
+16
source

If you also configured an action to return a partial view, you can also do:

@{ Html.RenderAction("PartialViewAction", "PartialViewCOntroller");}

This is probably better, since you should not be hard links to "Codes" in your code. Deploying a hard-coded link to another server may break the application, but there will be no action call to return the view.

+1
source

Source: https://habr.com/ru/post/899009/


All Articles