我正在将.NET MVC应用程序移植到.NET Core。使用Html.RenderPartial
渲染部分时会出现此问题。
相关代码是:
<div class="row">
<h2>@WebResources.OtherUsersSavedItems</h2>
@foreach (var item in Model.OtherUsersSavedItems)
{
Html.RenderPartial("SavedItem", Html.ViewDataDictionaryFrom(new { IsLink = true }, item));
}
</div>
模型上的OtherUsersSavedItems
属性定义为SavedItem[]
。在我的视图中有一个相同的RenderPartial调用,它有效,但区别在于它没有使用自定义的ViewDataDictionary:
<div class="row">
<h2>@WebResources.SavedItems</h2>
@foreach (var item in Model.SavedItems)
{
Html.RenderPartial("SavedItem", item);
}
</div>
ViewDataDictionaryFrom的代码如下:
public static ViewDataDictionary ViewDataDictionaryFrom(this IHtmlHelper helper, object dictionary, object model = null)
{
if (dictionary == null)
return null;
// Convert the object to a ViewDataDictionary
ViewDataDictionary vdd = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary());
foreach (var property in dictionary.GetType().GetProperties())
vdd.Add(property.Name, property.GetValue(dictionary));
vdd.Model = model;
return vdd;
}
以前的代码,在.NET MVC下运行时使用的是RenderPartialExtensions.RenderPartial(HtmlHelper, Strink, Object, ViewDataDictionary)
overload,而且运行正常。 .NET MVC和.NET Core之间针对ViewDataDictionaryFrom
方法进行了一处更改,即添加模型并相应地设置它(vdd.Model = model
)以尝试解决.NET Core中缺少的重载问题
我收到的例外是InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'MyApplication.Models.ListModel', but this ViewDataDictionary instance requires a model item of type 'MyApplication.Models.SavedItem'.
请注意,ListModel
是父视图的。{/ p>
堆栈帧(截断,从我的代码中的行开始失败)是:
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象 值) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary..ctor(的ViewDataDictionary source,object model,Type declaredModelType)lambda_method(Closure, ViewDataDictionary) Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.CreateViewDataDictionary(ViewContext 上下文) Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.Activate(对象 页面,ViewContext上下文) Microsoft.AspNetCore.Mvc.Razor.RazorPageActivator.Activate(IRazorPage 页面,ViewContext上下文) Microsoft.AspNetCore.Mvc.Razor.RazorView + d__16.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Mvc.Razor.RazorView + d__15.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)System.Runtime.CompilerServices.TaskAwaiter.GetResult() Microsoft.AspNetCore.Mvc.Razor.RazorView + d__14.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper + d__60.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperPartialExtensions.RenderPartial(IHtmlHelper htmlHelper,string partialViewName,ViewDataDictionary viewData) AspNetCore._Views_Index_cshtml + d__0.MoveNext() 在Index.cshtml中 Html.RenderPartial(“SavedItem”,Html.ViewDataDictionaryFrom(new {IsLink = true},item));
答案 0 :(得分:1)
我去看了GitHub上提供的源代码。超载
public static void RenderPartial(
this IHtmlHelper htmlHelper,
string partialViewName,
ViewDataDictionary viewData)
是RenderPartialAsync
的包装器,它使用当前模型调用它,忽略传入的ViewDataDictionary中的内容。
为了解决我的问题,我在Razor视图中使用RenderPartialAsync方法,这允许我指定ViewDataDictionary和模型。