문제

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

도움이 되었습니까?

해결책

You can use

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

Have a look at this page

다른 팁

What about RenderPartial? That seems to fit the bill.

<% Html.RenderPartial("MyPartialView", Model.Data); %>

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

Or maybe you want Templated Helpers?

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 ;-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top