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
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.
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.
You can clear the markup from the partial view using empty() : $('#EditTestSection'). empty();
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.
You need to add the .cshtml extension to the view name:
return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView.cshtml",PartialViewModel);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With