I am using @Html.Partial("Index") to call upon an action called "Index" in a controller, which should then return me the "Index" view. Unfortunately, it looks like the controller action is ignored because I get an error in Index view that Model is null. I tried setting breakpoints in the controller action and they are simply being skipped, it seems that the view "Index" is rendered without reading the controller? What is going on here?
Thx for any ideas.
Controller Action code is below:
public ActionResult Index()
{
int UserId = (int)Session["UserId"];
var Photos = db.Photos
.Where(Photo => Photo.ClientId == UserId)
.ToList();
if ((bool)Session["Admin"] == true) return PartialView(Photos);
else return View(Photos);
}
Html. Partial. Renders the partial view as an HTML-encoded string. This method result can be stored in a variable, since it returns string type value.
Partial the view is rendered with null, my action method is not called. Besides, two breakpoints and debugger also confirms, in latter case the my action method is not called. and call from main view is either @Html. Action("Test") or @Html.
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.
RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.
Html.Partial
will only render a view. If you want to call an action you must use Html.Action
, which will execute the action and return the view if any.
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