传递到字典中的模型项的类型为'',但此字典需要类型为'

时间:2015-08-19 06:18:06

标签: c# asp.net-mvc asp.net-mvc-4 razor

我收到以下错误:

  

传递到字典中的模型项是类型的   'Cygnus.Global.ViewModels.StoreViewModel',但是这本字典   需要类型的模型项   'Cygnus.Global.ViewModels.ProductOrderViewModel'。

以下是我的模型代码:

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

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


}

以下是我的控制器代码:

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();
}

以下是我的观看代码:

注意:StoreViewModel作为属性存在于ProductOrderViewModel中

@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;" })

2 个答案:

答案 0 :(得分:3)

我在控制器中纠正了我的错误

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();
}

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();
}

答案 1 :(得分:0)

你不了解错误?

您的View模型是Cygnus.Global.ViewModels.ProductOrderViewModel,但您从控制器模型对象传递的是StoreViewModel

我认为最好像这样编写你的控制器:

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();
}