What is difference between Html.Partial and Html.action in context of using a partial view in asp.net MVC3 ?
Html.Action will call a controller Action, so it'll go through the whole MVC pipeline (inside the server) again to find a controller that will return a ViewResult (although, you theoretically you can also return a JsonResult or something else) .
Html.Partial will only return a PartialPage (as in a CSHTML file) and won't go through the whole pipeline. It will just search using the view engine.
Some advantages to Action is having Authentication, Caching and other stuff that happens in the MVC pipeline, while Partial is faster (although you might have more responsibility in the partial page if you need to pass a ViewModel etc.)
This is a nice post (a bit old) about pros/cons of RenderAction vs RenderPartial
Html.Partial includes directly the view at the place where you called the helper. It is like a file include.
Html.Action invokes a controller action first which could render a view and it is the result of this action that is included. And because a controller action is invoked a controller needs to be instantiated, so the whole MVC pipeline gets executed as a child request.
You may take a look at the following blog post.
Stackoverflow ninja search got me this:
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
Also see this article
Render Action vs RenderPartial
Related
I have a web site written using ASP.NET MVC3.
The site has one single web page and what I want to do is to have the controller handle the request, do some parsing and processing and thereafter trigger a certain javascript method in the single webpage based on part of the data from the request.
How would I do this in the MVC controller?
You can't directly trigger actions in the View from the controller.
However, what you can do is return an ActionResult from the Controller Action to the View that instructs the View to execute your Javascript.
This could be a PartialView that includes the Javascript (in which case, you'll need a placeholder in your view for the PartialView to be rendered 'into') or something like a JSONResult that contains a property instructing the logic within the View what to do.
Either way, the call to the Controller Action is going to be triggered by client-side Javascript and your desired logic executed when the Controller Action finishes executing.
You'll probably find it easier to use jQuery.
When I'm in a View and I call #Html.RenderPartial("MyPartialView", MyObject) Can I set it up so that this partial view has a controller which is called when RenderPartial gets called?
Probably it will be better to use the RenderAction instead of the RenderPartial
You should gather all data necessary for the partial in the current controller action (which may use methods shared across other controllers and actions).
If you really want a partial to be rendered using its own controller/action then consider loading it via AJAX with a separate request.
In MVC, although controllers know about views, the reverse is not true.
Views are just means to render some data (a model or a viewModel) but they are not related to a controller or an action.
I'm porting an application from WebForms to MVC.
I have a WebForms UserControl that is used throughout the site and which contains presentational and business logic, and in particular some 'initizliation' logic (in the Page_Load)
My understanding of MVC UerControls is that they have no dedicated controller and thus contain no logic, and the logic is added to the controller of the viewpage to which the UserControl is added.
Now, I don;t want to have to write the initialization logic in my ViewPage, as this UserControl appears on several pages, thus duplication of logic.
I don't think I can user a MasterPage, as the UserControl can be rendered either vertically to the left of the page or hozontally at the top of the page depending on what page it is. This would require 2 MasterPage's, and again duplication of logic.
Do I have any other options?
This is a good case to use the RenderAction html helper. Much better, grab the Microsoft MVC futures and use the RenderAction method. Basically it lets you call a controller action within a view, which is alot more like how UserControls work than RenderPartial.
Bleh, I guess I should do it properly and pass in a object via my controller's viewdata rather than hacky-slash it.
Im having a value in ViewData, lets say htmlhelper.ViewData["myData"]="some";
And in partial page I can overwrite the myData's value.
But when I using the Html.RenderAction() and call a partial page.
In the same partial page htmlhelper.ViewData["myData"] is null.
When you call RenderAction, you create an entirely new ViewData instance for your partial page. If you want ViewData["myData"] to be visible by your other action, either pass it to the subaction or put it in TempData.
I figured out from MVC source code. Cool that we have MVC as open source.
htmlHelper.ViewContext.HttpContext.Items["myData"]
this will maintain the value from Partial and RenderAction case.
From what I have understood there is a big difference between the Html.RenderPartial included in the ASP.NET MVC release and the HTML.RenderAction in the Microsoft.Web.Mvc.ViewExtensions included in MVC Futures.
On my application I have many pages composed from many "widgets" (sort of) each having its own specific function.
It seemed to me more reasonable to use the RenderAction method as each widget would have a dedicated controller responsible for getting different data and rendering a dedicated view (as opposed to having only one controller and a unique view model to pass to RenderPartial helper to render views).
From the tests I have done having a form that points to a Create action method in a controller like:
<% using (Html.BeginForm("Create", "Message", FormMethod.Post,
new { id = "messageCreateForm" })) {%>
and calling it with
<% Html.RenderPartial("MessageForm",new MessageDTO()); %>
will render correcly a:
<form id="messageCreateForm" method="post" action="/Message/Create">
but with the same equivalent with RenderAction (so using a MessageForm action method on the controller to render the view) would not render correcly so:
<% Html.RenderAction<MessageController>(m => m.MessageForm()); %>
will render in:
<form id="messageCreateForm" method="post" action="">
Note that the action is empty.
Is this the correct way to use the RenderAction helper and is it correct to use it in cases like that?
UPDATE:
Actually renaming the partial view to _MessageForm renders the form correcly.
Very old one, but it jumped into my list of unanswered questions :)
There is a big difference between RenderAction and RenderPartial. RenderPartial will render a View on the same controller (or a shared one), while RenderAction will actually perform an entire cycle of MVC, that is: it will instantiate the controller (any controller you mention, not just the current one), it will execute the action, and it will then return and render the result.
The RenderPartial is more similar to an inclusion, it will even share the same model if you don't specify a different one.
The RenderAction is much more complex (and there may be undesired side effects, that's why they did not make this function available since version 1 -- initially it was available as an experimental feature).
So in your case, if you have widgets, it's OK to use both. It depends on the complexity of the widget. If you have one that has to get data from a DB, do something complex, etc... then you should probably use RenderAction.
I have a News controller responsible for news objects. I created a Block action, which will render a block with the latest news to be put in the home page. This is a perfect example, in my opinion, for RenderAction.
Working with MVC requires much attention to not to shoot yourself in the foot. I mean it by the efficiency of the MVC products. In complex projects I would prefer to use RenderPartial rather than RenderAction. I use RenderPartial in which I use jQuery.ajax request (with Html.Action). It definitely works more efficiently than RenderAction. By this way you can put your Views into cache and then call jQuery.ajax. Try it yourselves.
Ayende explains it clearly in Hibernating Rhinos.