Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.Partial() skips the controller action

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);
}
like image 240
RealityDysfunction Avatar asked Jun 02 '13 19:06

RealityDysfunction


People also ask

What does HTML partial do?

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.

What is the difference between HTML partial and HTML action?

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.

What is partial view in 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.

What is difference between RenderPartial and RenderAction?

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.


1 Answers

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.

like image 126
BrunoLM Avatar answered Sep 20 '22 11:09

BrunoLM