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

Posted: June 12, 2019 at 12:58 PM Quote #237603
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: June 20, 2019 at 2:10 AM Quote #238257
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.
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Thank's,
Sagar Kayasth

NopAdvance - nopCommerce Solution Partner
Website: https://nopadvance.com

We are native nopCommerce nerds...

For project inquiries: [email protected]
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
eCommerce CONFERENCE 2019
Learn more