asp.net-core - InvalidOperationException 在强类型 View 中呈现 ViewComponent

标签 asp.net-core asp.net-core-mvc .net-core asp.net-core-1.1

最近更新的 dotnet core 1.0.1 到 1.1 和 MVC 中的 ViewComponent 开始失败,出现以下异常:

InvalidOperationException: One or more errors occurred. (The model item passed into the ViewDataDictionary is of type 'App.Models.HomeViewModel', but this ViewDataDictionary instance requires a model item of type 'App.Components.LoginViewComponent'.)

Index.cshtml 呈现 登录 View 组件 :
@model App.Models.HomeViewModel
<html>
   @Component.InvokeAsync("LoginViewComponent")
</html>

View /主页/Index.cshtml

ViewComponent LoginViewComponent/Default.cshtml 只是一个显示其模型信息的类:
@model App.Components.LoginViewCompoent
<body>
    <div>@Html.DisplayFor(v=>v.LoginName)</div>
</body>

View /共享/组件/LoginViewComponent/Default.cshtml

时它呈现良好@model 从 Default.cshtml 中删除指令。

ViewComponent 不是应该与包装它的父 View 分开并且不可知吗?从异常(exception)情况来看,似乎需要声明 登录 View 组件 View 组件在 HomeViewModel 类以呈现它。

在 asp.net core github 上找不到关于此的任何更改说明。

对此的评论和帮助将不胜感激。

最佳答案

我遇到了同样的错误,我团队中有人更新了 Microsoft.AspNetCore.Mvc从 1.0.0 到 1.1.0 的版本以及我在 Strongly-Type View 中的一些组件开始抛出

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'My.StronglyView.ObjectType', but this ViewDataDictionary instance requires a model item of type 'My.ViewComponent.ObjectType'.



我不确定这种变化是否是故意的,但这绝对不是我们所期望的。

我没有时间研究这种“破坏性”更改的原因,但我提出了一个解决方案。

事情是,如果您将一个空对象传递给您的 ViewComponent.View()方法,我们得到这个异常。通过它传递的任何非空对象都会更新 ViewData.ModelExplorer 并且正确的对象类型将被注册,从而避免此异常。

使用 Tester-Doer 模式,与 .Net Framework 的某些类中使用的模式相同,我们现在可以将非空对象传递给 ViewComponent并根据需要使用它及其包装的对象。

我所做的是,我创建了一个界面 IViewComponentModel<T>作为类(class)ViewComponentModel<T>如下:
// Interface for ViewComponentModel
public interface IViewComponentModel<T> 
    where T : class
{
    T Data { get; }
    bool HasData();
}

// ViewComponentModel class for the Strongly-Type View Component
public class ViewComponentModel<T> : IViewComponentModel<T>
    where T : class
{
    public T Data { get; private set; }

    public ViewComponentModel(T data)
    {
        Data = data;
    }

    public bool HasData() => Data != null;
}

在我的 ViewComponent 类实现中,我返回 View(new ViewComponentModel<AnyReferenceType>(myModel));
public async Task<IViewComponentResult> InvokeAsync()
{
    var myViewData = _myService.GetSomeObjectForMyViewComponent();
    var model = new ViewComponentModel<MyViewDataType>(myViewData);

    return await Task.FromResult(View(model));
}

最后,在我的组件 View (.cshtml) 中,我有以下代码:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model ViewComponentModel<MyViewDataType>

现在你可以对 View 中的这个对象做任何你想做的事情并使用你的真实模型,只需调用 @Model.Data瞧。

这样,我们将永远不会将空对象传递给 ViewComponent,它也不会“继承”View 的对象类型。

希望它有帮助!

关于asp.net-core - InvalidOperationException 在强类型 View 中呈现 ViewComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40721574/

相关文章:

asp.net-core-mvc - ASP NET Core 2.0 appsettings.Development.json 不适用于日志记录配置

c# - 在 ASP.net 5 MVC 6 中,如何在不同的命名空间中使用相同的 Controller 名称

iis - ASP.NET Core 无法在 IIS 下运行 : HTTP Error 500. 0 - ANCM In-Process Handler Load Failure

concurrency - 并行异步任务不同时执行

asp.net-core - 如何在.net core中使用.(variable_name)访问类属性?

asp.net-core - 如何在 ASP.net vNext 中引用不同文件夹中的项目

angular - 如何在前端和后端存储 JWT 刷新 token ?

asp.net - 在 ASP.NET MVC Core Controller 的构造函数中设置 ViewBag 属性

c# - AntiForgeryToken 到期空白页

javascript - HttpDelete 和参数