简体   繁体   中英

The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

I am getting following error:

The model item passed into the dictionary is of type 'Cygnus.Global.ViewModels.StoreViewModel', but this dictionary requires a model item of type 'Cygnus.Global.ViewModels.ProductOrderViewModel'.

Following is my Model code:

public class StoreViewModel
{
       public int Id { get; set; }

       public List<StoreViewModel> Stores { get; set; }


}

Following is my Controller Code:

public ActionResult StoreProducts (StoreViewModel model)
{
      CygnusInternalResponseViewModel response = new CygnusInternalResponseViewModel();
      response = new Logic(CApplicationId, CurrentCompanyId).GetProductsByStoreId(model.Id);
      var parentmodel = new ProductOrderViewModel() { Products = response.Model, Orders = new OrderViewModel() };
      if (response.Success)
          return View(model);

      return View();
}

And following is my View Code:

NOTE: StoreViewModel exists in ProductOrderViewModel as a property

@model Cygnus.Global.ViewModels.ProductOrderViewModel
@foreach (var pd in Model.Products)
            {
            <p>
             span class="cmtText"> | @pd.Name | @pd.UnitPrice </span>

            </p>
             }
            @Html.LabelFor(m => m.Orders.SubTotal)
            @Html.TextBoxFor(m => m.Orders.SubTotal, new { @readonly = "readonly", @style = "width:100px; float:right;margin-top:-21px;" })

I corrected my mistake in controller

public ActionResult StoreProducts (StoreViewModel model)
{
      CygnusInternalResponseViewModel response = new CygnusInternalResponseViewModel();
      response = new Logic(CApplicationId, CurrentCompanyId).GetProductsByStoreId(model.Id);
      var parentmodel = new ProductOrderViewModel() { Products = response.Model, Orders = new OrderViewModel() };
      if (response.Success)
          return View(model);

      return View();
}

To

public ActionResult StoreProducts (StoreViewModel model)
{
      CygnusInternalResponseViewModel response = new CygnusInternalResponseViewModel();
      response = new Logic(CApplicationId, CurrentCompanyId).GetProductsByStoreId(model.Id);
      var parentmodel = new ProductOrderViewModel() { Products = response.Model, Orders = new OrderViewModel() };
      if (response.Success)
          return View(parentmodel );

      return View();
}

What you don't get about error?

Your View model is Cygnus.Global.ViewModels.ProductOrderViewModel , but you pass from controller model object witch is StoreViewModel .

I gess it's better to write your controller like this:

public ActionResult StoreProducts (StoreViewModel model)
{
      CygnusInternalResponseViewModel response = new CygnusInternalResponseViewModel();
      response = new Logic(CApplicationId, CurrentCompanyId).GetProductsByStoreId(model.Id);
      var parentmodel = new ProductOrderViewModel() { Products = response.Model, Orders = new OrderViewModel() };
      if (response.Success)
          ViewData.Model = parentmodel //This is right model

      return View();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM