简体   繁体   English

ASP.NET MVC ViewResult与PartialViewResult

[英]ASP.NET MVC ViewResult vs PartialViewResult

What is the difference between the controller result named ViewResult and PartialViewResult ? 名为ViewResultPartialViewResult的控制器结果有什么区别? More importantly, when is the PartialViewResult used? 更重要的是,何时使用PartialViewResult

PartialViewResult is used to render a partialview (fx. just a user control). PartialViewResult用于渲染部分视图(fx。只是用户控件)。 This is pretty nifty for AJAX stuff, ie 这对于AJAX的东西来说非常好玩,即

<script type="text/javascript">
    $.get(
        "/MyController/MyAction",
        null,
        function (data) { $("#target").html(data) }
     );
</script>

and action 和行动

public ActionResult MyAction() 
{
    return PartialView("SomeView");
}

where SomeView is a MVC User Control, eg: 其中SomeView是MVC用户控件,例如:

<div>
   <%= DateTime.Now.ToString() %>
</div>

http://msmvps.com/blogs/luisabreu/archive/2008/09/16/the-mvc-platform-action-result-views.aspx http://msmvps.com/blogs/luisabreu/archive/2008/09/16/the-mvc-platform-action-result-views.aspx

In practice, you'll use the PartialViewResult for outputing a small part of a view. 实际上,您将使用PartialViewResult输出视图的一小部分。 That's why you don't have the master page options when dealing with them. 这就是为什么在处理它们时没有母版页选项的原因。 On the other hand, you'll use the ViewResult for getting a “complete” view. 另一方面,您将使用ViewResult获取“完整”视图。 As you might expect, the Controller class exposes several methods that will let you reduce the ammount of typing needed for instanting these types of action results. 正如您所料,Controller类公开了几种方法,可以减少为即时执行这些类型的操作结果所需的输入量。

Generally speaking, ViewResult is for rendering a page with optional master, and PartialViewResult is used for user controls (likely responding to an AJAX request). 一般来说,ViewResult用于呈现具有可选主控的页面,而PartialViewResult用于用户控件(可能响应AJAX请求)。

none of the existing answers actually answer the question "What is the difference". 现有的答案都没有真正回答“有什么不同”的问题。

The differences are as follows: 差异如下:

1) the locations where the view engine will attempt to find the view: 1)视图引擎将尝试查找视图的位置:

  • for ViewResult, it's in ViewLocationFormats and MasterLocationFormats 对于ViewResult,它位于ViewLocationFormats和MasterLocationFormats中
  • for PartialViewResult, it's in PartialViewLocationFormats 对于PartialViewResult,它位于PartialViewLocationFormats中

2) ViewResult has the additional property MasterName 2)ViewResult具有附加属性MasterName

that is all. 就这些。

There are several cases where you would want to break down your view into several small components. 在某些情况下,您可能希望将视图分解为几个小组件。 One use case that I am working with right now, is I have a multi-lingual site that I would like to reload content using AJAX principles. 我正在使用的一个用例是,我有一个多语言网站,我想使用AJAX原则重新加载内容。

Normally what I would do in the case of a non-multi lingual site is to create another ActionResult to return the ViewModel that is changing with the new parameters. 通常,在非多语言站点的情况下,我要做的是创建另一个ActionResult来返回使用新参数更改的ViewModel。 I like to use a custom ActionResult that I have called JsonpResult. 我喜欢使用我称之为JsonpResult的自定义ActionResult。 The problem resides in the fact that I have labels not in my database but in Resource files. 问题在于我的标签不在我的数据库中而是在资源文件中。 So what I would need to do is to somehow hydrate my Resource file data into the ViewModel. 所以我需要做的是以某种方式将我的资源文件数据保存到ViewModel中。

Once the data comes down the pipe, my AJAX callback handles the wiring up of the ViewModel response back to the HTML page using Javascript (I use jQuery). 一旦数据从管道下来,我的AJAX回调就会使用Javascript处理将ViewModel响应连接回HTML页面(我使用jQuery)。

This definitely works, however it becomes a question of maintainability. 这绝对有效,但它成为可维护性的问题。 I now need to not only maintain my original ASP.NET view, but I also need to maintain a set of scripts that handle AJAXian behavior. 我现在不仅需要维护原始的ASP.NET视图,还需要维护一组处理AJAXian行为的脚本。 If you need to have your site SEO, then you really need to make sure that both the Server Side and Client Side behavior are both working the same. 如果您需要拥有自己的网站搜索引擎优化,那么您确实需要确保服务器端和客户端的行为都是相同的。

This is where Partial Views come into play for me. 这是Partial Views为我发挥作用的地方。 What I do is "pull out" the logical data sections where the bulk of the reload occurs. 我所做的是“拉出”大量重载发生的逻辑数据部分。 The nice thing about PartialView is that you can pass your ViewData and Model along to the PartialView. PartialView的优点在于您可以将ViewData和Model传递给PartialView。 If your PartialView is strongly typed against your ViewModel you can get Intellisense to help with the wiring of the PartialView. 如果您的部分视图是针对ViewModel强类型的,那么您可以使用Intellisense来帮助连接PartialView。

Now all I need to do with my AJAX call is to write the response back to a single DIV rather than handling data points individually. 现在我需要对我的AJAX调用做的就是将响应写回单个DIV而不是单独处理数据点。 What it does mean is that there would be more content coming down the pipe. 它的意思是会有更多的内容流入管道。 However, the trade off is easier to read and maintain code. 但是,权衡更容易阅读和维护代码。

The one of the main differences is PartialViewResult doesn't use _ViewStart.cshtml. 其中一个主要区别是PartialViewResult不使用_ViewStart.cshtml。 The code from _ViewStart.cshtml file executes at the start of rendering before any code in the view. _ViewStart.cshtml文件中的代码在视图中的任何代码之前的渲染开始时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM