Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 Razor Templates VS RenderPartial

Tags:

I just read this blog post on Razor Templating in ASP.NET MVC 3.

Put simply, i just dont get it!

That is, i don't see why we need this (fairly) complicated code to achieve what can be done IMO easier (and neater) with @RenderPartial ?

Here's what i don't like:

  1. The template is stored as a Func<T,HelperResult> delegate?
  2. That template delegate is persisted in the Controller ViewData (e.g HttpContext.Current.Items)

The only "benefit" i read from that blog is that a seperate file isn't required for templating, meaning you don't need to re-compile etc.

But i don't see that as a valid argument. Extra files are fine as long as the solution organization isn't compromised.

I prefer using @RenderPartial, as i can keep my markup seperate from the master view, and i can render this both inline (render time) and with jQuery (e.g AJAX event).

Maybe i'm missing something here, but can anyone give some reasons why we should choose Razor Templating over RenderPartial to create re-usable content?

like image 399
RPM1984 Avatar asked Dec 08 '10 02:12

RPM1984


People also ask

What is the benefit of HTML RenderPartial using ASP.NET MVC Razor engine?

What is the benefits of Html. RenderPartial using ASP.Net MVC Razor Engine? A) @Html. RenderPartial Returns response, moreover requires to create action.

What is the difference between RenderPartial and partial in MVC?

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 are the benefits of HTML RenderPartial over?

Use RenderPartial when you have a model to send to the view and there will be a lot of html that doesn't need to be stored in a variable. Use Partial when you have a model to send to the view and there will be a little bit of text that needs to be stored in a variable. RenderAction and RenderPartial are faster.

What is MVC RenderPartial?

RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template."


1 Answers

Well, you should ask the author of that post about his motivation for presenting this technique.

It certainly illustrates what is possible in Razor. Whether you should use it is a different matter. I personally think that there are alternative techniques that are less complicated (I agree with your points about storing a Func inside of the request context).

  • There's @RenderPartial which you already mentioned.
  • You can also use the @helper syntax (either as a local helper or a global helper)
  • You can write an html helper (and use TagBuilder to assemble the output)
  • You can write a child action
  • You can write a templated helper

Now that I look at the above list I think MVC might provide too much choice :)

Update To better illustrate how inline templates can be useful I wrote a blog post about using them to call sections with default code: Optional Razor Sections with Default Content.

You can use it to write something like this:

@this.RenderSection("OptionalSection", @<div>Default Content</div>)  
like image 186
marcind Avatar answered Sep 29 '22 08:09

marcind