gpt4 book ai didi

asp.net-mvc - "return View()"和 "return PartialView()"有什么区别

转载 作者:行者123 更新时间:2023-12-03 07:05:22 25 4
gpt4 key购买 nike

我知道部分 View 用于渲染 View 的一部分。但我不明白 return View()return PartialView() 之间有什么区别以及什么时候使用它们。

最佳答案

返回View() - 渲染 .aspx|.cshtml 页面

  • 呈现一个普通的 .aspx 页面,也可以包含部分 View

返回PartialView() - 渲染.ascx|.cshtml控件

  • 向浏览器呈现一段 HTML,可通过 AJAX 或非 AJAX 请求进行请求。

View() 返回ViewResultPartialView() 返回 PartialViewResult两者都继承自 ViewResultBase

差异由 Reflector 描述下面...

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;
}
}
}

enter image description here

关于asp.net-mvc - "return View()"和 "return PartialView()"有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2662285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com