[RESOLVED]Search Partial View in the given folder

Hi All,

I am giving the path of partial view in my view as -

@Html.Partial("partial view path", Model)

If partial view not found in the given path then i have to find it in "Shared" folder. So for this requirement i saw in some posts that they are opting to have the customized View Engine.
Do i have any other option for this ? 

this is standard functionality. 

the viewengine first looks in the working directory (for example if you are on the homecontroller/index and you load a partial into that page.)

@Html.Partial("_somepartial", Model);  looks in views/home/_somepartial.cshtml

if it can’t find it in views/home/ then it will look into /views/shared/_somepartial.cshtml

if it has to act differently you would have to customize the view engine like you suggested there is no other option i think


just wondering why you want to deviate from the default MVC provides to you. If you put your view in the folder under Views with the same name as the Controller then it picks up automatically. If you want to reuse the partial view for other controllers you
can place it in the Shared subfolder.

Another approach might be to make use of Display or Editor templates.

Grz, Kris.

You can derive from RazorViewEngine and modify default locations

I agree but in my case i have folder like "Partial View" instead of Controller Name. It will be like -

@Html.Partial("_somepartial", Model);  looks in views/partial views/_somepartial.cshtml

try this

@if (System.IO.File.Exists(Server.MapPath("~/Views/PartialViews") + @"partialviewname.cshtml"))
    @Html.Partial("~/Views/PartialViews/PartialViewName.cshtml", Model);

I think you are looking for 


<div class="line number2 index1 alt1"> var
controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();</div> <div class="line number3 index2 alt2">  string
cLayout = "";</div> <div class="line number4 index3 alt1"> if
(controller == "Webmaster") {</div> <div class="line number5 index4 alt2"> cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";</div>
<div class="line number6 index5 alt1"> }</div> <div class="line number7 index6 alt2"> else
{</div> <div class="line number8 index7 alt1"> cLayout = "~/Views/Shared/_Layout.cshtml";</div>
<div class="line number9 index8 alt2"> }</div> <div class="line number10 index9 alt1"> Layout = cLayout;</div> <div
class="line number11 index10 alt2">}</div> <div class="line number11 index10 alt2"></div> <div class="line number11 index10 alt2">for more details

click here

Leave a Reply