partial view was not found or no view engine suppo

2019-01-25 01:53发布

问题:

I have following controller code:

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

    [ChildActionOnly]
    public ActionResult MyPartialViewAction()
    {
        return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView",PartialViewModel);
    }
}

and my Index view has the following code :

@HTML.Action("MyPartialViewAction")

When I run the Web app I get HttpException with InnerExceptionMessage as :

InnerException {"The partial view '~/Views/Shared/MyCustomFolder/_MyPartialView' was not found or no view engine supports the searched locations. The following locations were searched:\r\n~~/Views/Shared/MyCustomFolder/_MyPartialView"} System.Exception {System.InvalidOperationException}

What I have tried till now :

  • Tried moving _MyPartialView from ~/Views/Shared/MyCustomFolder to ~/Views/Shared/ and ~/Views/MyControllerFolder but still error exists

  • Tried changing my Index View code to @HTML.RenderAction() but no luck.

Any inputs on where I'm going wrong ?

Thanks

回答1:

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

return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel);


回答2:

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.



回答3:

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);



回答4:

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.



回答5:

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