Asp.Net Mvc 2 可重用部分?

发布于 2024-08-07 12:48:30 字数 409 浏览 5 评论 0原文

我想知道,是否有与 Asp.Net MVC 的 Monorail View 组件等效的组件?

我想做的是渲染一些依赖于类的复杂 UI,所以假设我们在列表视图中,我想将一个对象传递给此 ViewComponent 等效项,并且它将将该对象作为参数并渲染复杂的 UI我的 UI 允许我在视图中做其他事情。在 Asp.Net MVC 中执行此操作的最佳方法是什么?

由于这是一个复杂的 UI,我更愿意编写一次,因此模板并不是我真正想要的方式,因为这意味着我将遇到可维护性问题。
我看到的一些选项(我即将开始尝试)是:

  • Html.RenderAction in the futures
  • Subcontroller

但是我想知道是否还有其他选项,或者对于这个特定场景,一个选项是否比另一个更好

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对你的占有欲 2024-08-14 12:48:30

您可以使用

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

查看此页面< /a>

You can use

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

Have a look at this page

猫性小仙女 2024-08-14 12:48:30

渲染部分怎么样?这似乎符合要求。

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

What about RenderPartial? That seems to fit the bill.

<% Html.RenderPartial("MyPartialView", Model.Data); %>
少跟Wǒ拽 2024-08-14 12:48:30

请查看 MVC 2 预览中的 DisplayTemplates 和 EditorTemplates。

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

○愚か者の日 2024-08-14 12:48:30

或者您可能需要模板化助手

Or maybe you want Templated Helpers?

情魔剑神 2024-08-14 12:48:30

MvcContrib InputBuilder 有类似的东西。

尽管您可以自己编写:

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

注意递归。当然,您必须在 switch(PropertyType) 内添加集合支持等...但这并不难。您还可以检查属性上的 UIHint 以呈现部分。很多可能性,一切都在你的掌控之中;-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文