【问题标题】:ViewDataDictionary model is correct, failing when calling RenderPartialViewDataDictionary 模型正确,调用 RenderPartial 时失败
【发布时间】:2018-05-26 16:10:37
【问题描述】:

我正在将 .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 并且运行良好。对于 ViewDataDictionaryFrom 方法,.NET MVC 和 .NET Core 之间有一个变化,即添加模型并相应地设置它 (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 是父视图的。

堆栈帧(截断,从我的代码中失败的行开始)是:

Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object 价值) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary..ctor(ViewDataDictionary 源,对象模型,类型声明模型类型) lambda_method(闭包, 查看数据字典) 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、字符串 partialViewName、ViewDataDictionary viewData) AspNetCore._Views_Index_cshtml+d__0.MoveNext() 在 Index.cshtml 中 Html.RenderPartial("SavedItem", Html.ViewDataDictionaryFrom(new { IsLink = true }, item));

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc asp.net-core-2.0


    【解决方案1】:

    我去查看了GitHub 上的源代码。超载

    public static void RenderPartial(
            this IHtmlHelper htmlHelper,
            string partialViewName,
            ViewDataDictionary viewData)
    

    RenderPartialAsync 的包装器,它使用当前模型调用它,忽略传入的 ViewDataDictionary 中的内容。

    为了更正我的问题,我在 Razor 视图中使用了 RenderPartialAsync 方法,它允许我指定 ViewDataDictionary 和模型。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2017-09-26
      • 2018-06-16
      • 2018-09-03
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多