我正在按照此处列出的教程: http://dotnetawesome.blogspot.com/2014/07/nested-webgrid-with-expand-collapse-in-aspnet-mvc4.html使用我的VS2015企业IDE。
但是,在步骤8,我右键单击OrderController中的List()ActionResult并选择Add View with the following:
我收到以下错误:
但是在本教程中,他们没有使用OrderVM的密钥。即使我确实添加[Key]
并且正确使用并且在重建之后我仍然得到相同的错误,并且我完全不知道为什么给我使用getter和setter。
的ViewModels: OrderVM
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using WebAppTest.Models;
namespace WebAppTest.ViewModels
{
public class OrderVM
{
[Key]
public OrderMaster order { get; set; }
public List<OrderDetail> orderDetails { get; set; }
}
}
模型(从DB执行CodeFirst生成): 为MyModel
namespace WebAppTest.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class MyModel : DbContext
{
public MyModel()
: base("name=MyDatabaseEntities")
{
}
public virtual DbSet<OrderDetail> OrderDetails { get; set; }
public virtual DbSet<OrderMaster> OrderMasters { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OrderDetail>()
.Property(e => e.Product)
.IsUnicode(false);
modelBuilder.Entity<OrderDetail>()
.Property(e => e.Rate)
.HasPrecision(10, 2);
modelBuilder.Entity<OrderDetail>()
.Property(e => e.Amount)
.HasPrecision(10, 2);
modelBuilder.Entity<OrderMaster>()
.Property(e => e.OrderAmount)
.HasPrecision(10, 2);
modelBuilder.Entity<OrderMaster>()
.Property(e => e.CustomerName)
.IsUnicode(false);
modelBuilder.Entity<OrderMaster>()
.Property(e => e.CustomerAddress)
.IsUnicode(false);
}
}
}
的OrderDetail
namespace WebAppTest.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class OrderDetail
{
[Key]
public int OrderDetailsID { get; set; }
public int OrderID { get; set; }
[Required]
[StringLength(100)]
public string Product { get; set; }
public int Quantity { get; set; }
[Column(TypeName = "numeric")]
public decimal Rate { get; set; }
[Column(TypeName = "numeric")]
public decimal Amount { get; set; }
}
}
OrderMaster
namespace WebAppTest.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("OrderMaster")]
public partial class OrderMaster
{
[Key]
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
[Column(TypeName = "numeric")]
public decimal OrderAmount { get; set; }
[Required]
[StringLength(100)]
public string CustomerName { get; set; }
[StringLength(200)]
public string CustomerAddress { get; set; }
}
}
控制器
OrderController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebAppTest.Models;
using WebAppTest.ViewModels;
namespace WebAppTest.Controllers
{
public class OrderController : Controller
{
// GET: Order
//public ActionResult Index()
//{
// return View();
//}
public ActionResult List()
{
List<OrderVM> allOrder = new List<OrderVM>();
// here MyDatabaseEntities is our data context
using (MyModel dc = new MyModel())
{
var o = dc.OrderMasters.OrderByDescending(a => a.OrderID);
foreach (var i in o)
{
var od = dc.OrderDetails.Where(a => a.OrderID.Equals(i.OrderID)).ToList();
allOrder.Add(new OrderVM { order = i, orderDetails = od });
}
}
return View(allOrder);
}
}
}
答案 0 :(得分:2)
以下是Scaffolder检查关键字段的逻辑:
首先找到具有此条件的所有属性:
然后尝试根据名称找到密钥:
尝试根据属性找到关键字:
Key
属性EdmScalarProperty
的属性,其值为EntityKeyPropety=true
Column
的属性,其值为IsPrimaryKey=true
因此,您应该在课堂上提交符合上述标准之一的文件
此外,您应该从[Key]
属性中删除order
,因为复杂类型不能是密钥。
作为选项,您可以将此属性添加到您的班级:
[Key]
public int OrderID { get; set; }
作为另一种选择,您的OrderVM
课程可以是这样的:
public partial class OrderVM
{
public OrderVM()
{
orderDetails = new List<OrderDetails>();
}
[Key]
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public decimal OrderAmount { get; set; }
[Required]
[StringLength(100)]
public string CustomerName { get; set; }
[StringLength(200)]
public string CustomerAddress { get; set; }
public List<OrderDetail> orderDetails { get; set; }
}
答案 1 :(得分:0)
我刚遇到这个问题。我尝试添加[Key]
,我使用了public string Id { get; set; }
。最终,解决方法是我只需要重建项目即可。我想在这里留下这个答案,以防万一将来有人遇到这个问题。