Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderAction RenderPartial

Tags:

asp.net-mvc

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.

like image 562
Ronnie Avatar asked Apr 05 '09 14:04

Ronnie


People also ask

What is the difference between RenderPartial and partial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

What is RenderAction in MVC?

RenderAction(HtmlHelper, String) Invokes the specified child action method and renders the result inline in the parent view. RenderAction(HtmlHelper, String, Object) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view.

What is RenderPartial?

RenderPartial returns void. Html. Partial injects the html string of the partial view into the main view. Html. RenderPartial writes html in the response stream.

What is the difference between render () and RenderPartial ()? Write the syntax?

Renderpartial does exactly the same thing and is better for performance over partial(). The main difference between Partial and RenderPartial is : Partial return string and write it to document as Shweta said . RenderPartial actually write direct to context response. Html.


1 Answers

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.

like image 79
Palantir Avatar answered Sep 23 '22 14:09

Palantir