Tuesday 10 October 2017

ASP.NET MVC 4 partial view

Your call:
@Html.Partial("_StatePartial")
will render the view, but it will not call the action. You would only use this if you were seeting up all your view data in your parent page action. In your case you need to use this:
@Html.Action("_StatePartial")
This will call the action first to retrieve and execute the view.




Because Html.Partial directly renders a partial view with the passed model (optionally). I mean directly that it's not going though the MVC lifecycle, only acts like rendering a "template".
If you want to run a child action, which is going through the entire lifecycle (acts like a real request) then use Html.Action method. This way your controller action will be invoked, including the routing system, filters, etc.
One should consider which method he should use, because the Html.Action is much slower of course (even if most likely the bottleneck will not be here). I used to think this way: for reusing views use Html.Partial, for reusing both business logic and view use Html.Action.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.