I have a controller, action which returns PartialViewResult and view with it. For testing I output current DateTime (in action), and in view I check if it is null or not, so I know what I got.
When I try to “embed” that view in another one with Html.Action I get current datetime, so my action is called.
But when I use Html.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.
Action method:
public PartialViewResult Test()
{
return PartialView(DateTime.Now);
}
(partial) View:
@model DateTime?
<p>@(Model ?? DateTime.MinValue)</p>
and call from main view is either @Html.Action("Test") or @Html.Partial("Test").
Html.Action() will call your action method, but Html.Partial() will not. Html.Partial() just renders your partial view, and is useful if you have some static content, or if you’ve already loaded the view data.
Will render the PartialName view with your model data passed to it. It’s a great way to break up views into clean sections, without having to incur any additional requests to the server.
will call your Test action, and render the result.
This is why you’re seeing the NULL DateTime. Html.Action() is actually calling the action, computing the DateTime, and rendering the view, whereas Html.Partial() is only rendering the view.