Search code examples
c#.netasp.net-mvcasp.net-core

Strange .NET Core Partial View loading issue using a built string


A bit of background is that I'm trying to load a Navigation partial file per Sub Area that I'm in. Simple I originally thought...

Okay, so this works:

@await Html.PartialAsync("~/Areas/Admin/SubAreas/General/Views/Shared/_Navigation.cshtml")

And this does not work:

@if (ViewContext.RouteData.Values.ContainsKey("subarea"))
{
    // ViewContext.RouteData.Values["subarea"].ToString() comes out as "General"
    await Html.PartialAsync("~/Areas/Admin/SubAreas/" + ViewContext.RouteData.Values["subarea"].ToString() + "/Views/Shared/_Navigation.cshtml");
}      

I've tried removing the if statement to see if that was something to do with it, nope. I've tried string formatting like:

await Html.PartialAsync(string.Format("~/Areas/Admin/SubAreas/{0}/Views/Shared/_Navigation.cshtml", ViewContext.RouteData.Values["subarea"].ToString()));

Still no banana - the string definitely comes out as "~/Areas/Admin/SubAreas/General/Views/Shared/_Navigation.cshtml", and if I try changing this to a path that's invalid then it throws an error, so it's definitely finding the View, but just refuses to display it.

I can't seem to find anything about this around the net, but may be searching for the wrong thing. Any ideas out there?!


Solution

  • Try putting the @ back before the await partial.

    I had issues with something like this last week and scratched my head for a while.

    I think without the @ the view contents is kept code side and not marshalled back into the callers view.