首页 > 其他分享 >return View() vs returnPartialView()

return View() vs returnPartialView()

时间:2023-05-09 14:25:25浏览次数:57  
标签:return HTML vs result PartialView returnPartialView page View

return View() vs returnPartialView()

In ASP.NET MVC, both return View() and return PartialView() are used to return a view result from a controller action method.

return View() renders the full HTML layout, including the layout page, while return PartialView() renders only the HTML for the view itself. return PartialView() is typically used when the view is intended to be inserted into another page, or when you only want to update a portion of the page using Ajax.

For example, if you have a view that shows a list of products and you want to use Ajax to update the list without refreshing the whole page, you might use return PartialView() to render the updated product list HTML, which can be inserted into the page using JavaScript. On the other hand, if you want to render a full page with header, footer and other layout elements, you would use return View().

In summary, return View() is used to render a full view including layout, while return PartialView() is used to render just the HTML for the view itself, which is typically used when using Ajax or updating only a portion of the page.

 

What's the difference between "return View()" and "return PartialView()"

回答1

return PartialView() returns HTML code fragment and it is used with ViewUserControls - ASCX files. The main advantage of using "return PartialView()" is when you don't want to render all the other HTML page stuff, like HTML, BODY, HEAD tags.

One of the most common uses by me is when I want to render just the user control based on whether the request to an action is AJAX call.

So I have a View called MyControl.aspx where I use RenderPartial HTML helper and I have a partial View named MyControl.ascx where I do the actual render.

I can switch between those two in my controller action like this:

if (Request.IsAjaxRequest())
    return PartialView("MyControl"); // this renders MyControl.ascx

return View(); // this render MyControl.aspx

 

回答2

Return View() - Renders an .aspx|.cshtml page

  • Renders a normal .aspx page that can also contain Partial Views

Return PartialView() - Renders .ascx|.cshtml Control

  • Renders a segment of HTML to the browser that can be requested through AJAX or Non-AJAX requests alike.

View() returns ViewResult PartialView() returns PartialViewResult both inherit from ViewResultBase

The difference is described by Reflector below...

public class PartialViewResult : ViewResultBase
{
   // Methods
   protected override ViewEngineResult FindView(ControllerContext context)
   {
      ViewEngineResult result = base.ViewEngineCollection.FindPartialView(context, base.ViewName);
      if (result.View != null)
      {
         return result;
      }
      StringBuilder builder = new StringBuilder();
      foreach (string str in result.SearchedLocations)
      {
         builder.AppendLine();
         builder.Append(str);
      }
      throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, MvcResources.Common_PartialViewNotFound, new object[] { base.ViewName, builder }));
   }
}


public class ViewResult : ViewResultBase
{
   // Fields
   private string _masterName;

   // Methods
   protected override ViewEngineResult FindView(ControllerContext context)
   {
      ViewEngineResult result = base.ViewEngineCollection.FindView(context, base.ViewName, this.MasterName);
      if (result.View != null)
      {
         return result;
      }
      StringBuilder builder = new StringBuilder();
      foreach (string str in result.SearchedLocations)
      {
         builder.AppendLine();
         builder.Append(str);
      }
      throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, MvcResources.Common_ViewNotFound, new object[] { base.ViewName, builder }));
   }

   // Properties
   public string MasterName
   {
      get
      {
         return (this._masterName ?? string.Empty);
      }
      set
      {
         this._masterName = value;
      }
   }
}

 

标签:return,HTML,vs,result,PartialView,returnPartialView,page,View
From: https://www.cnblogs.com/chucklu/p/17384844.html

相关文章

  • @Html.Partial vs @Html.Action
    @Html.Partialand@Html.ActionarebothusedinASP.NETMVCtoincludereusablecontentinaview.@Html.Partialrendersapartialviewdirectly.It'susefulforrenderingsmallandsimpleviews,[email protected]......
  • macOS下使用VS Code配置C/C++开发环境
      macOS系统中默认的C/C++编译器是clang/clang++,命令行使用gcc/g++或者clang/clang++来执行命令时,都是调用clang/clang++编译器,想使用gcc/g++编译器结合VSCode进行开发的话,我们可以自己安装。查看一下gcc和clang   1.安装Homebrew,官网链接,复制粘贴到Terminal运行脚......
  • vsftpd虚拟用户
    创建虚拟用户密码文件[root@web01vsftpd]#pwd/etc/vsftp[root@web01vsftpd]#catvir_usershyhftpshyhftpwebftpHTCH@trade#2014生产虚拟用户数据库[root@web01vsftpd]#db_load-T-thash-fvir_uservir_user.db[root@web01vsftpd]#chmod700/etc/vsftpd/vi......
  • 做了个vscode 小插件,用于修改window 的颜色以区分同时打开的不同工作区,快用起来吧!
    Coralizemarketplace/coralize以高效且便捷的方式自定义VisualStudioCode工作区窗口的状态栏、标题栏以及活动边栏等颜色!这将对那些需要同时打开多个vscode窗口/工作区的人非常有用。Coralize提供了一系列中国传统文化色彩,并搭配友好的用户界面。Customizethecolorscheme......
  • VScode代码自动补全提示
    VScode代码自动补全提示1.打开设置2.搜索Suggest:SnippetsPreventQuickSuggestions,去掉勾选。3.Ctrl+Shift+P打开setting.json文件,添加以下代码//vscode不自动补全添加代码"editor.suggest.snippetsPreventQuickSuggestions":false,"editor.quickSugg......
  • VsCode设置插件安装目录
    VsCode设置插件安装目录如果是还没有安装插件,希望把新的插件安装到新目录,可以使用【命令行/快捷方式/资源管理器右键】的方式来打开vscode如果之前已经安装过插件,可以把C:\Users\xxxx\.vscode下的extensions目录拷贝到插件需要安装的路径下,再进行下面的操作命令行方式可以......
  • vs2010单元测试
    一、     实验目的1、 掌握单元测试技术,并按单元测试的要求设计测试用例。 2、 掌握一种单元测试工具的使用。二、 实验内容自行学习vs2010或vs2012或vs2015等单元测试工具的使用。对下面被测代码进行测试且查看代码覆盖率,并录制操作视频,撰写实验报告。三、 设......
  • VSCode常用插件记录
    1.[Deprecated]BracketPairColorizer2 2.C/C++3.C/C++ClangCommandAdapter4.C/C++ExtensionPack5.C/C++Themes6.GitBlame7.GitGraph8.GitHistory9.GitLens—Gitsupercharged10.IntelliCode11.PartialDiff12.snippets13.LocalHistory14.change-c......
  • 双端VSC-HVDC直流输电仿真模型
    双端VSC-HVDC直流输电仿真模型matlab2021a,采用双环控制,电压环和电流环,电压环采用直流电压参与PI调节,电流内环包括PI调节器与前馈解耦,整流侧与逆变侧为VSC两电平器件。直流波形输出效果理想,交流侧电压电流均为对称的三相电压电流,ID:2545684639072616......
  • 文心一言 VS chatgpt (15)-- 算法导论3.2 4~5题
    四、函数$\lceil\lgn\rceil!$多项式有界吗?函数$\lceil\lg\lgn\rceil!$多项式有界吗?文心一言:chatgpt:对于第一个问题,函数$\lceil\lgn\rceil!$是阶乘的形式,可以证明它是超多项式增长的,因此不是多项式有界的。对于第二个问题,函数$\lceil\lg\lgn\rceil!$......