Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 429
NaN Avatar asked Feb 26 '14 21:02

NaN


People also ask

How do I add a partial view in view?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

What is partial view in MVC C#?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

How do I clear partial view?

You can clear the markup from the partial view using empty() : $('#EditTestSection'). empty();


2 Answers

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.

like image 73
Sanvir Manilal Avatar answered Oct 07 '22 16:10

Sanvir Manilal


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

return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel); 
like image 43
Maess Avatar answered Oct 07 '22 18:10

Maess