处理 _Partial Page 请求时发生未处理的异常
An unhandled exception occurred while processing the request for _Partial Page
首页级别视图被命名为 "Create.cshtml"
在该页面上,我需要放置多个 _partialViews.cshtml,这是我使用 razor 页面制作的:
<div>
<row>
<div class="col-md-6">
@await Html.PartialAsync("~/Views/Shared/_PartialDelegates")
</div>
<div class="col-md-6">
@await Html.PartialAsync("~/Views/Shared/_PartialApps")
</div>
</row>
</div>
现在的问题是我正在尝试用 _PartialDelegates.cs
调用 _PartialDelegates.cshtml
_PartialDelegates.cshtml
@page
@model IMP.ECR.WEB.Views.Shared._PartialDelegatesModel
@{
}
<partial name="_PartialDelegatesView" model="Model.Avengers" />
调用 _PartialDelegates.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace IMP.ECR.WEB.Views.Shared
{
public class _PartialDelegatesModel : PageModel
{
public List<string> Avengers = new List<string>();
public void OnGet()
{
Avengers.AddRange(new[] { "Spiderman", "Iron Man", "Dr. Stange", "The Hulk" });
}
}
}
}
然后在 _PartialDelegatesView.cshtml 我有以下内容:
@model List<string>
<div class="col-md-12">
<h2>Avengers</h2>
<table class="table table-boardered table-striped" style="width:100%">
@foreach (var item in Model)
{
<tr>
<td>@item</td>
</tr>
}
</table>
</div>
但是,我一直收到未处理的异常 ---
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an
object.
AspNetCore.Views_Shared__PartialDelegates.get_Model()
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Shared__PartialDelegates.get_Model()
AspNetCore.Views_Shared__PartialDelegates.ExecuteAsync() in _PartialDelegates.cshtml
+
<partial name="_PartialDelegatesView" model="Model.Avengers" />
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.RenderPartialCoreAsync(string partialViewName, object model, ViewDataDictionary viewData, TextWriter writer)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.PartialAsync(string partialViewName, object model, ViewDataDictionary viewData)
AspNetCore.Views_Sponsors_Create.ExecuteAsync() in Create.cshtml
+
@await Html.PartialAsync("~/Views/Shared/_PartialDelegates.cshtml")
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync<TFilter, TFilterAsync>()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
您需要删除 _PartialDelegates.cshtml
中的 @page
指令才能使局部视图正常工作。但问题是 _PartialDelegatesModel
OnGet
方法从未被调用,因此 Avengers
列表未被实例化。
似乎无法将像这样的 razor 页面用作局部视图,只有常规的 razor 视图适合此目的。如果您需要在呈现页面之前实例化一些值,那么根据 docs ("Important" section)
If you need to execute code, use a view component instead of a partial view.
加载 Razor 页面作为局部视图似乎是不可能的,请参阅 。
Razor 页面不能用作 View Components 的替代品。请按照以下步骤创建一个 PartialDelegates
ViewComponent,它可以执行您想要的相同操作。
1.Create一个ViewComponens
文件夹并在文件夹
中添加一个PartialDelegates.cs
class
public class PartialDelegates:ViewComponent
{
public IViewComponentResult Invoke()
{
List<string> Avengers = new List<string>();
Avengers.AddRange(new[] { "Spiderman", "Iron Man", "Dr. Stange", "The Hulk" });
return View(Avengers);
}
}
2.AddDefault.cshtml
到路径/Views/Shared/Components/PartialDelegates/Default.cshtml
(文件夹不存在则创建)
@model List<string>
<h1>Default</h1>
<partial name="_PartialDelegatesView" model="Model" />
3.In 你的 Create.cshtml,调用视图组件
@await Component.InvokeAsync("PartialDelegates")
首页级别视图被命名为 "Create.cshtml"
在该页面上,我需要放置多个 _partialViews.cshtml,这是我使用 razor 页面制作的:
<div>
<row>
<div class="col-md-6">
@await Html.PartialAsync("~/Views/Shared/_PartialDelegates")
</div>
<div class="col-md-6">
@await Html.PartialAsync("~/Views/Shared/_PartialApps")
</div>
</row>
</div>
现在的问题是我正在尝试用 _PartialDelegates.cs
调用 _PartialDelegates.cshtml_PartialDelegates.cshtml
@page
@model IMP.ECR.WEB.Views.Shared._PartialDelegatesModel
@{
}
<partial name="_PartialDelegatesView" model="Model.Avengers" />
调用 _PartialDelegates.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace IMP.ECR.WEB.Views.Shared
{
public class _PartialDelegatesModel : PageModel
{
public List<string> Avengers = new List<string>();
public void OnGet()
{
Avengers.AddRange(new[] { "Spiderman", "Iron Man", "Dr. Stange", "The Hulk" });
}
}
}
}
然后在 _PartialDelegatesView.cshtml 我有以下内容:
@model List<string>
<div class="col-md-12">
<h2>Avengers</h2>
<table class="table table-boardered table-striped" style="width:100%">
@foreach (var item in Model)
{
<tr>
<td>@item</td>
</tr>
}
</table>
</div>
但是,我一直收到未处理的异常 ---
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an
object.
AspNetCore.Views_Shared__PartialDelegates.get_Model()
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Shared__PartialDelegates.get_Model()
AspNetCore.Views_Shared__PartialDelegates.ExecuteAsync() in _PartialDelegates.cshtml
+
<partial name="_PartialDelegatesView" model="Model.Avengers" />
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.RenderPartialCoreAsync(string partialViewName, object model, ViewDataDictionary viewData, TextWriter writer)
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.PartialAsync(string partialViewName, object model, ViewDataDictionary viewData)
AspNetCore.Views_Sponsors_Create.ExecuteAsync() in Create.cshtml
+
@await Html.PartialAsync("~/Views/Shared/_PartialDelegates.cshtml")
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync<TFilter, TFilterAsync>()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
您需要删除 _PartialDelegates.cshtml
中的 @page
指令才能使局部视图正常工作。但问题是 _PartialDelegatesModel
OnGet
方法从未被调用,因此 Avengers
列表未被实例化。
似乎无法将像这样的 razor 页面用作局部视图,只有常规的 razor 视图适合此目的。如果您需要在呈现页面之前实例化一些值,那么根据 docs ("Important" section)
If you need to execute code, use a view component instead of a partial view.
加载 Razor 页面作为局部视图似乎是不可能的,请参阅 PartialDelegates
ViewComponent,它可以执行您想要的相同操作。
1.Create一个ViewComponens
文件夹并在文件夹
PartialDelegates.cs
class
public class PartialDelegates:ViewComponent
{
public IViewComponentResult Invoke()
{
List<string> Avengers = new List<string>();
Avengers.AddRange(new[] { "Spiderman", "Iron Man", "Dr. Stange", "The Hulk" });
return View(Avengers);
}
}
2.AddDefault.cshtml
到路径/Views/Shared/Components/PartialDelegates/Default.cshtml
(文件夹不存在则创建)
@model List<string>
<h1>Default</h1>
<partial name="_PartialDelegatesView" model="Model" />
3.In 你的 Create.cshtml,调用视图组件
@await Component.InvokeAsync("PartialDelegates")