加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 在强类型视图中呈现ViewComponent的InvalidOper

发布时间:2020-12-16 06:40:12 所属栏目:asp.Net 来源:网络整理
导读:最近更新的dotnet核心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 ViewDataD
最近更新的dotnet核心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呈现LoginViewComponent:

@model App.Models.HomeViewModel
<html>
   @Component.InvokeAsync("LoginViewComponent")
</html>

查看/主页/ Index.cshtml

ViewComponent LoginViewComponent / Default.cshtml只是一个显示模型信息的类:

@model App.Components.LoginViewCompoent
<body>
    <div>@Html.DisplayFor(v=>v.LoginName)</div>
</body>

查看/共享/组件/ LoginViewComponent / Default.cshtml

从Default.cshtml中删除@model指令时,它呈现正常.

是不是ViewComponent假设与包装它的父视图分开并且不可知?从异常看,似乎需要在HomeViewModel类中声明LoginViewComponent ViewComponent才能呈现它.

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

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

解决方法

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

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’.

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

我没有时间研究这种“突破”变化的原因,但我找到了一个解决方案.

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

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

我做的是,我创建了一个接口IViewComponentModel< T>作为类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));
}

最后,在我的组件视图(.cshtml)中,我有以下代码:

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
@model ViewComponentModel<MyViewDataType>

现在,您可以在视图内部使用此对象执行任何操作,并使用您的真实模型,只需调用@ Model.Data和voilà即可.

这样,我们永远不会将null对象传递给ViewComponent,也不会从View中“继承”对象类型.

希望它有所帮助!

asp.net-core asp.net-core-mvc dotnet-core asp.net-core-1.1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读