1

我在将包含少量属性的模型传递给视图时遇到问题。使用此代码,我收到错误

type 'viewmodel 命名空间中不存在类型名称Entity

控制器

public ActionResult Index()
   {
       return View(GetProducts());
   }
private static IEnumerable<ProductViewModel> GetProducts()
   {
       var northwind = new NorthwindDataContext();
       return northwind.Products.Select(product => new ProductViewModel
       {
           ProductID = product.ProductID,
           ProductName = product.ProductName,
           UnitPrice = product.UnitPrice ?? 0,
           UnitsInStock = product.UnitsInStock ?? 0,
           UnitsOnOrder = product.UnitsOnOrder ?? 0,
           Discontinued = product.Discontinued,
           LastSupply = DateTime.Today
       });
   }

视图模型

namespace AutoComplete.Entity
{
 public class ProductViewModel
 {
    [ScaffoldColumn(false)]
    public int ProductID
    {
        get;
        set;
    }

    [Required]
    [DisplayName("Product name")]
    public string ProductName
    {
        get;
        set;
    }

    [Required]
    [DisplayName("Unit price")]
    [DataType(DataType.Currency)]
    [Range(0, int.MaxValue)]
    public decimal UnitPrice
    {
        get;
        set;
    }

    [Required]
    [DisplayName("Units in stock")]
    [DataType("Integer")]
    [Range(0, int.MaxValue)]
    public int UnitsInStock
    {
        get;
        set;
    }

    public bool Discontinued
    {
        get;
        set;
    }

    [DisplayName("Last supply")]
    [DataType(DataType.Date)]
    public DateTime LastSupply
    {
        get;
        set;
    }

    public short UnitsOnOrder { get; set; }
}
}

看法

@model IEnumerable<AutoComplete.Entity.ProductViewModel>
4

0 回答 0