views:

497

answers:

5

Hi there:

I wonder, is there an equivalent of the Monorail View components for Asp.Net MVC?

What I m trying to do is render some complex UI that depends on a class, so say we are in a List view, I want to pass an object to this ViewComponent equivalent and that it will take the object as a parameter and render the complex UI for me allowing me to do other stuff in the view. What would be the best way to do that in Asp.Net MVC?

Since this is a complex UI I would prefer to write it once, hence templates are not really the way I would like to go, as it will mean I ll have maintainability issues.
Some options I saw ( and I m about to start trying) are:

  • Html.RenderAction in the futures
  • Subcontroller

However I d like to know if there is anything else or if one is better than the other for this particular scenario

A: 

Please take a look at DisplayTemplates as well as EditorTemplates in MVC 2 preview.

twk
I dont think they are an option as that would generate code for each view, what I want is to put the "complexUI" once in one view
Miau
+2  A: 

What about RenderPartial? That seems to fit the bill.

<% Html.RenderPartial("MyPartialView", Model.Data); %>
mgroves
Thanks its another way but find RenderAction more powerful
Miau
A: 

MvcContrib InputBuilder has something similar.

Though you may just write your own:

public static string RenderInput(this HtmlHelper html, object data, string prefix)
{
   foreach (var prop in data.GetType().GetProperties())
   {
      object val = prop.GetValue(data, new object[0]);
      string name = prefix + prop.Name;
      switch (prop.PropertyType.Name)
      {
          case "String": html.TextBox(name, val); break;
          case "Guid": html.Hidden(name, val); break;
          default: html.RenderInput(val, name + "."); break;
      }
   }
}

Notice recursion. Of course you will have to add collections support, etc... inside switch(PropertyType)... but this is not that hard. You may also check for UIHint on the property to render partials. A lot of possibilities and all under your control ;-)

queen3
+2  A: 

You can use

<% Html.RenderAction<ProductController>(c => c.RenderProductResults()); %>

Have a look at this page

San
San, Viewcomponents supports two pass processing (Initialize and Render methods which allows) that won't be achieved by calling a renderview.Also, in my thinking, they don't serve the same purpose, I find the concepts quite differents.I'm using my own wrappers for jquery ui and that's look like that in my markup: <datepicker id="someid" date="view.SomeDate" />I think that would look really funky with RenderActionwith asp.net mvc, using UserControl (or System.Web.Control) would allow reuse of small components as well as viewcomponents, but this solution is tied to WebForm viewengine.
smoothdeveloper
A: 

Or maybe you want Templated Helpers?

mgroves