【发布时间】:2014-10-16 17:23:00
【问题描述】:
我有下面的代码。当我运行它时,我得到了异常:
System.Data.Entity.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理
附加信息:无法创建“MvcApplication1.Models.QuantityPerUnit”类型的常量值。此上下文仅支持原始类型或枚举类型。”
好像我不能在里面使用List
public static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit>。它应该替换为 IEnumerable,但如果我使用 IEnumerable,它会显示比较错误:
System.Collections.Generic.IEnumerable' 不包含“添加”的定义
代码:
namespace MvcApplication1.Models
{
public class ProductEditViewModel : Product
{
// For DropDownListFor need IEnumerable<SelectListItem>
public IEnumerable<SelectListItem> SupplierItems { get; set; }
// For RadioButtonFor need below
public Int32? CategorySelectedId { get; set; }
public IEnumerable<Category> CategorieItems { get; set; }
// For CheckBoxFor need below
public string QuantityPerUnitSelectedId { get; set; }
public IEnumerable<QuantityPerUnit> QuantityPerUnitItems { get; set; }
}
public class QuantityPerUnit
{
public string QuantityPerUnitId { get; set; }
public string Quantity { get; set; }
public static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit>
{
new QuantityPerUnit { QuantityPerUnitId = "1", Quantity = "10" },
new QuantityPerUnit { QuantityPerUnitId = "2", Quantity = "20" },
new QuantityPerUnit { QuantityPerUnitId = "3", Quantity = "25" },
new QuantityPerUnit { QuantityPerUnitId = "4", Quantity = "50" },
new QuantityPerUnit { QuantityPerUnitId = "5", Quantity = "100" }
};
}
}
[HttpGet]
public ActionResult ProductEdit(Int32 ProductId)
{
var northwind = new NorthwindEntities();
var q = from p in northwind.Products
where p.ProductID == ProductId
select new ProductEditViewModel
{
ProductID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice,
Discontinued = p.Discontinued,
SupplierItems = from sup in northwind.Suppliers
select new SelectListItem
{
Text = sup.CompanyName,
Value = SqlFunctions.StringConvert((double)sup.SupplierID),
Selected = sup.SupplierID == p.SupplierID
},
CategorySelectedId = p.CategoryID,
CategorieItems = from cat in northwind.Categories select cat,
QuantityPerUnitSelectedId = p.QuantityPerUnit,
QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems
};
return View(q.SingleOrDefault());
}
<div class="form-group">
@Html.LabelFor(model => model.QuantityPerUnit)
@foreach(var Quantity in MvcApplication1.Models.QuantityPerUnit.QuantityPerUnitItems)
{
@Html.CheckBoxFor(model => model.QuantityPerUnitSelectedId == Quantity.QuantityPerUnitId, Quantity.QuantityPerUnitId)
}
</div>
【问题讨论】:
标签: c# asp.net asp.net-mvc entity-framework linq-to-entities