partial view was not found or no view engine supports the searched locations

后端 未结 6 1936
野的像风
野的像风 2020-12-17 07:22

I have following controller code:

public MyController:Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
         


        
相关标签:
6条回答
  • 2020-12-17 08:04

    You need to add the .cshtml extension to the view name:

    return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel);
    
    0 讨论(0)
  • 2020-12-17 08:05

    If the answers are still not working for you and you have put your partial in a nested folder inside of partials, ensure that you provide the full path to the partial with the extension.

    @Html.Partial("~/Views/Partials/Components/_AdvancedComponentRenderer.cshtml", Model.Content);

    0 讨论(0)
  • 2020-12-17 08:08

    I got this error message because the partial view was added to the project file (.csproj) but was not actually present on disk.

    0 讨论(0)
  • 2020-12-17 08:10

    Also double check for spaces after your filename, before the ".cshtml". I had inserted one blank space accidentally after my filename, and this disrupted the automatic behavior of PartialView() for me.

    0 讨论(0)
  • 2020-12-17 08:12

    Just in case the selected answer is not working for you:

    I changed the build action of the cshtml file to Content and it fixed it.

    0 讨论(0)
  • 2020-12-17 08:22

    Hope, this code helps you (In ".cshtml" file)

    @using Microsoft.AspNetCore.Mvc.ViewEngines
    @inject ICompositeViewEngine Engine
    
    ....
    string path = "../Themes/User/_head";
    
    @if (Engine.FindView(ViewContext, path, isMainPage: false).Success)
    {
        <partial name="@path" for="@Model" />
    }
    else
    {
        <partial name="../Themes/Admin/_head" />
    }
    

    Thanks

    0 讨论(0)
提交回复
热议问题