The model item passed into the ViewDataDictionary is of type 'Castle.Proxies.Model' in nopcommerce 4.2

7 months ago
This error throws me when I am pass data to the model and call the view.

Here is the full error shows,

An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Castle.Proxies.VendorProxy', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IList`1[Nop.Web.Models.Common.VendorDetailModel]'.

Now, I create one model, one view and controller in nopcommerce 4.2.

Here is my model place,

Nop.Web => Models => Common => VendorDetailModel

Here is the code of model

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

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

Here is the controller placed

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

Here is the controller code

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

Here is the view placed,

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

Here is the view code

@model VendorDetailModel
content.......

So, this error is showing when I place the @model VendorDetailModel in view file while if I remove this line then error is not showing. But I remove this line then how can I get the value without model.
7 months ago
sksoni wrote:
This error throws me when I am pass data to the model and call the view.

Here is the full error shows,

An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Castle.Proxies.VendorProxy', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IList`1[Nop.Web.Models.Common.VendorDetailModel]'.

Now, I create one model, one view and controller in nopcommerce 4.2.

Here is my model place,

Nop.Web => Models => Common => VendorDetailModel

Here is the code of model

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

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

Here is the controller placed

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

Here is the controller code

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

Here is the view placed,

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

Here is the view code

@model VendorDetailModel
content.......

So, this error is showing when I place the @model VendorDetailModel in view file while if I remove this line then error is not showing. But I remove this line then how can I get the value without model.


Hello sksoni
You have to prepare VendorDetailModel.
because Vednordetail method in you get vendor entity in model.
so vendor entity record prepare with VendorDetailModel.

var model = new VendorDetailModel();
var vendor = _vendorService.GetVendorById(vendorId);
model.Name = vendor.name;
var address = _addressService.GetAddressById(vendor?.AddressId ?? 0);
if(address != null)
{
  var addressModel = new AddressModel
  {
    Id = address.Id,
    // prepare address model related fields
  };
  model.Address.add(addressModel);
}
return View("Vendordetail",model);


hope you get your solution.