我的代码如下。当我运行它时,我得到了例外:
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>
答案 0 :(得分:1)
您需要将要转换为SQL的部分分离出来,并由数据库从您希望在应用程序端完成的部分执行。
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,
//remove this from here
//QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems
})
.AsEnumerable()
.Select(p => new ProductEditViewModel
{
p.ProductID,
//all of the other properties
QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems,
};
答案 1 :(得分:0)
马修。
从数据库获取项目时,使用IQueryable(T)类型进行操作。 此类型转换为SQL脚本。 这种LINQ被称为 - Linq to entities。
要执行操作,您需要使用Linq to Objects。
您需要在方法AsEnumerable()的帮助下将查询转换为IEnumerable(T),然后将静态数据添加到ViewModel。