MVC2 中的 Html.Partial(view, model) 和 Html.RenderPartial(view,model) 之间有什么区别(如果有的话)? [英] What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

查看:17
本文介绍了MVC2 中的 Html.Partial(view, model) 和 Html.RenderPartial(view,model) 之间有什么区别(如果有的话)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了它返回的类型以及您当然对它的称呼不同这一事实

Other than the type it returns and the fact that you call it differently of course

<% Html.RenderPartial(...); %>
<%= Html.Partial(...) %>  

如果它们不同,为什么要调用一个而不是另一个?
定义:

If they are different, why would you call one rather than the other one?
The definitions:

// Type: System.Web.Mvc.Html.RenderPartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll

using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    public static class RenderPartialExtensions
    {
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName);
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData);
        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model);

        public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model,
                                     ViewDataDictionary viewData);
    }
}

// Type: System.Web.Mvc.Html.PartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll

using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    public static class PartialExtensions
    {
        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName);

        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName,
                                        ViewDataDictionary viewData);

        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model);

        public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model,
                                        ViewDataDictionary viewData);
    }
}

推荐答案

唯一不同的是 Partial 返回一个 MvcHtmlString,并且必须在 < 内部调用;%= %>,而 RenderPartial 返回void 并直接渲染到视图.

The only difference is that Partial returns an MvcHtmlString, and must be called inside <%= %>, whereas RenderPartial returnsvoid and renders directly to the view.

如果您查看源代码,您会发现它们都调用相同的内部方法,传递一个 StringWriter 以供渲染.

If you look at the source code, you'll see that they both call the same internal method, passing a StringWriter for it to render to.

如果您想查看、保存或操作生成的 HTML 而不是将其写入页面,您可以调用 Partial.

You would call Partial if you want to view, save, or manipulate the generated HTML instead of writing it to the page.

这篇关于MVC2 中的 Html.Partial(view, model) 和 Html.RenderPartial(view,model) 之间有什么区别(如果有的话)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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