MVC中的Html.Partial和Html.RenderPartial有什么区别? [英] What is Difference between Html.Partial and Html.RenderPartial in MVC?

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

问题描述

MVC中的Html.Partial和Html.RenderPartial有什么区别?

推荐答案

- Html.Partial 返回字符串 Html.RenderPartial 返回 void

- 我们可以将Html.Partial的输出存储在一个变量/能够从函数返回。

- 在Html.RenderPartial中,我们不能导致void。(即,直接写到输出流所以它比html.partial()快一点。



Html.RenderPartial

1.此方法结果将直接写入HTTP响应流意味着它使用与当前网页/模板中使用的相同的TextWriter对象。

2.此方法返回void。

3.简单易用,无需创建任何操作。

4.当部分视图中的显示数据已经存在于相应的视图模型中时,RenderPartial方法非常有用。例如:In一个博客,以显示一篇文章的评论,我们希望使用RenderPartial方法,因为视图模型中已经填充了带注释的文章信息。

@ {Html.RenderPartial(_ Comments);}

5.此方法比Partial方法更快,因为它的结果直接写入响应流,这使得它更快。



< b> Html.Partial

1.将部分视图渲染为HTML编码的字符串。

2.此方法结果可以存储在变量中,因为它返回字符串类型值。

3.简单易用,无需创建任何操作。

4.与RenderPartial方法一样,Partial方法在显示时也很有用局部视图中的数据已经在相应的视图模型中。例如:在显示文章评论的博客中,您可以使用部分方法,因为视图模型中已经填充了带注释的文章信息。

@ Html.Partial (_评论)



请参阅 -

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor [ ^ ]





-KR
- Html.Partial returns a string, Html.RenderPartial returns void.
- We can store the output of Html.Partial in a variable/able to return from function.
- In Html.RenderPartial, we can’t result void.(ie.,directly writing to the output stream so it was bit faster than html.partial())

Html.RenderPartial
1. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.
2. This method returns void.
3. Simple to use and no need to create any action.
4. RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.
@{Html.RenderPartial("_Comments");}
5. This method is faster than Partial method since its result is directly written to the response stream which makes it fast.

Html.Partial
1. Renders the partial view as an HTML-encoded string.
2. This method result can be stored in a variable, since it returns string type value.
3. Simple to use and no need to create any action.
4. Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model. For example: In a blog to show comments of an article, you can use Partial method since an article information with comments are already populated in the view model.
@Html.Partial("_Comments")

Refer this-
RenderPartial vs RenderAction vs Partial vs Action in MVC Razor[^]


-KR


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

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