首页 > 解决方案 > 传入 ViewDataDictionary 的模型项在 nopcommerce 4.2 中属于“Castle.Proxies.Model”类型

问题描述

当我将数据传递给模型并调用视图时,这个错误会抛出我。

这是完整的错误显示,

处理请求时发生未处理的异常。InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“Castle.Proxies.VendorProxy”,但此 ViewDataDictionary 实例需要类型为“System.Collections.Generic.IList`1[Nop.Web.Models.Common.VendorDetailModel”的模型项]'。

现在,我在 nopcommerce 4.2 中创建了一个模型、一个视图和一个控制器。

这里是我的模范地方,

Nop.Web => 模型 => 通用 => VendorDetailModel

这是模式的代码

public VendorDetailModel()
{
    Address = new List<AddressModel>();
}
public string Name { get; set; }       

public IList<AddressModel> Address { get; set; }

这是放置的控制器

Nop.Web => Controllers => CommonController => Vendordetail(method)

这是控制器代码

public virtual IActionResult Vendordetail(int vendorId)
{
    var model = _vendorService.GetVendorById(vendorId);
    return View("Vendordetail",model);
}

这是放置的视图,

Nop.Web => Views => Common => Vendordetail.cshtml

这是查看代码

@model VendorDetailModel
content.......

因此,当我将@model VendorDetailModel放在视图文件中时会显示此错误,而如果我删除此行则不会显示错误。但是我删除了这条线,那么我如何在没有模型的情况下获得价值。

标签: c#asp.net-mvcnopcommerce

解决方案


也许在您看来,您将错误的模型传递给局部视图。就我而言,我的视图正在获取“BOX”的模型,并使用期望“ORDER”模型的局部视图,但我没有明确地将“ORDER”模型传递给局部,因此局部视图得到了默认的“BOX”模型,这是错误的并触发了这个“Castle.model.proxy”错误。


推荐阅读