.NET MVC 응용 프로그램을 .NET 코어로 이식하고 있습니다. Html.RenderPartial
을 사용하여 부분 렌더링 할 때 문제가 발생합니다.RenderPartial을 호출 할 때 ViewDataDictionary 모델이 정확하지 않습니다.
관련 코드는 다음과 같습니다 모델에
<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을 사용하고 있었고 정상적으로 작동했습니다. ViewDataDictionaryFrom
메서드의 경우 .NET MVC와 .NET Core 사이에 한 가지 변경 사항이있었습니다.이 메서드는 모델을 추가하고 .NET 코어의 누락 된 과부하를 해결하기 위해 그에 맞게 설정했습니다 (vdd.Model = model
).
내가받는 예외는 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
은 상위보기입니다.
스택 프레임 (절단은 실패 코드 내에서 행에서 시작하는)이다
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible (객체 값) Microsoft.AspNetCore.Mvc .ViewFeatures.ViewDataDictionary..ctor (ViewDataDictionary 소스, 객체 모델, 유형 declaredModelType) lambda_method (폐쇄, ViewDataDictionary) Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.CreateViewDataDictionary (ViewContext 컨텍스트) Microsoft.AspNetCore.Mv c.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 (예외 작업) System.Runtime.CompilerServices. TaskAwaiter.GetResult() Microsoft.AspNetCore.Mvc.Razor.RazorView + d__14.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (작업 작업) 마이크로 소프트 .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() in Index.cshtml Html.RenderPartial ("SavedItem", Html.ViewDataDictionaryFrom (new {IsLink = true}, item)));