代码之家  ›  专栏  ›  技术社区  ›  CoffeeCode

如何在HTML助手中调用renderAction?

  •  2
  • CoffeeCode  · 技术社区  · 16 年前

    我正在扩展htmlhelper。但我不能称之为它的渲染。

     using System.Text;
     using System.Web.Mvc;
     using System.Web.Mvc.Html;
     public static class ViewHelpers
        {
            public static string Text(this HtmlHelper htmlHelper, string name, object value, bool isEditMode)
            {
               htmlHelper.RenderAction(...) //cannot be called
            }
        }
    

    我怎样才能称之为渲染????

    3 回复  |  直到 13 年前
        1
  •  3
  •   sonjz    13 年前

    Coffeecode在正确的轨道上,但要包含的命名空间应为 系统.web ,请 系统.Web.MVC system.web.mvc.html(系统.web.mvc.html) .

    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using System.Resources;
    using System.Web.Routing;
    using System.Text.RegularExpressions;
    
    namespace xxx.yyy.Helpers {
        public static class HelperView {
            public static void RenderHeader(this HtmlHelper helper) {
                helper.RenderAction("Header", "Default", new { pageAction = helper.ViewContext.RouteData.Values["action"].ToString() });
            }
        }
    }
    

    http://msdn.microsoft.com/en-us/library/ee703490(v=vs.108).aspx

        2
  •  2
  •   griegs    16 年前

    @咖啡豆,你能解释一下你为什么要这样做吗?

    一般来说,您会调用一个助手,将HTML作为字符串返回,然后在页面中呈现。

    想要在助手中呈现它似乎有点奇怪。至少对我来说。

    编辑

    我想这里的帮手可能对你来说是错误的。如果您想访问列表,但不想将其传递给视图,那么助手是错误的。您可能需要创建一个类来获取列表,对其进行填充,然后返回HTML。

        3
  •  -2
  •   Leniel Maccaferri    15 年前

    解决办法真的很简单。
    添加 using Microsoft.Web.Mvc;
    当呼叫 RenderAction 它将把HTML字符串直接写入视图

    推荐文章