ASP.NET MVC使用渲染来自内部的HTML帮助部分 [英] ASP.NET MVC Using Render Partial from Within an Html Helper

查看:103
本文介绍了ASP.NET MVC使用渲染来自内部的HTML帮助部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用返回一个字符串生成器和复杂的逻辑相当数量的字符串的扩展的HtmlHelper。我现在想添加一些额外的它,取自渲染部分呼叫,这样的事情...

I have an HtmlHelper extension that currently returns a string using a string builder and a fair amount of complex logic. I now want to add something extra to it that is taken from a render partial call, something like this ...

public static string MyHelper(this HtmlHelper helper)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("Hi There");
    builder.Append(RenderPartial("MyPartialView"));
    builder.Append("Bye!");
    return builder.ToString();
}

当然现在的RenderPartial直接呈现到反应,使这并未;:T工作,我尝试了几种解决方案呈现给分音弦,但似乎都摔倒了,我会用上内部分的的HtmlHelper

Now of course RenderPartial renders directly to the response so this doesn;t work and I've tried several solutions for rendering partials to strings but the all seem to fall over one I use the HtmlHelper within that partial.

这可能吗?

推荐答案

由于这个问题,虽然老标回答说,在谷歌出现了,我要去给一个不同的答案。

Because this question, although old and marked answered, showed up in google, I'm going to give a different answer.

在asp.net MVC 2和3,还有工作方式类似于的RenderPartial但返回的局部视图作为字符串,而不是直接渲染它的Html.Partial(...)方法。

In asp.net mvc 2 and 3, there's an Html.Partial(...) method that works like RenderPartial but returns the partial view as a string instead of rendering it directly.

您的例子也因此成为:

//using System.Web.Mvc.Html;
public static string MyHelper(this HtmlHelper helper)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("Hi There");
    builder.Append(helper.Partial("MyPartialView"));
    builder.Append("Bye!");
    return builder.ToString();
}

这篇关于ASP.NET MVC使用渲染来自内部的HTML帮助部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆