是什么在MVC2 Html.Partial(视图模型)和Html.RenderPartial(视图模型)之间的差额(如果有的话)? [英] What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

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

问题描述

除了返回类型和调用不同的看法,当然

的事实

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

如果他们是不同的,为什么你的一个电话而不是其他呢?结果
定义:

  //类型:System.Web.Mvc.Html.RenderPartialExtensions
//大会:System.Web.Mvc,版本= 2.0.0.0,文化=中性公钥= 31bf3856ad364e35
//大会位置:C:\\ Program Files文件(x86)的\\微软ASP.NET \\ ASP.NET MVC 2 \\组件\\ System.Web.Mvc.dll程序使用System.Web.Mvc;命名空间System.Web.Mvc.Html
{
    公共静态类RenderPartialExtensions
    {
        公共静态无效的RenderPartial(此的HtmlHelper的HtmlHelper,串partialViewName);
        公共静态无效的RenderPartial(此的HtmlHelper的HtmlHelper,串partialViewName,可视数据的ViewDataDictionary);
        公共静态无效的RenderPartial(此的HtmlHelper的HtmlHelper,串partialViewName,对象模型);        公共静态无效的RenderPartial(此的HtmlHelper的HtmlHelper,串partialViewName,对象模型,
                                     可视数据的ViewDataDictionary);
    }
}//类型:System.Web.Mvc.Html.PartialExtensions
//大会:System.Web.Mvc,版本= 2.0.0.0,文化=中性公钥= 31bf3856ad364e35
//大会位置:C:\\ Program Files文件(x86)的\\微软ASP.NET \\ ASP.NET MVC 2 \\组件\\ System.Web.Mvc.dll程序使用System.Web.Mvc;命名空间System.Web.Mvc.Html
{
    公共静态类PartialExtensions
    {
        公共静态MvcHtmlString部分(此的HtmlHelper的HtmlHelper,串partialViewName);        公共静态MvcHtmlString部分(此的HtmlHelper的HtmlHelper,串partialViewName,
                                        可视数据的ViewDataDictionary);        公共静态MvcHtmlString部分(此的HtmlHelper的HtmlHelper,串partialViewName,对象模型);        公共静态MvcHtmlString部分(此的HtmlHelper的HtmlHelper,串partialViewName,对象模型,
                                        可视数据的ViewDataDictionary);
    }
}


解决方案

唯一的区别是,部分返回 MvcHtmlString ,并且必须调用在<%=%> ,而的RenderPartial 收益无效,并直接呈现到视图。

如果你看一下源$ C ​​$ C ,你会看到,他们都调用相同的内部方法,传递一个StringWriter的为它渲染到。

您会叫部分如果您想要查看,保存或处理生成的HTML,而不是将其写入到页面。

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.NET\ASP.NET MVC 2\Assemblies\System.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.NET\ASP.NET MVC 2\Assemblies\System.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);
    }
}

解决方案

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

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.

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(视图模型)和Html.RenderPartial(视图模型)之间的差额(如果有的话)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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