“未定义键";为视图模型创建视图时出现错误消息 [英] "No Key Defined" error message, when creating a view for a view model

查看:76
本文介绍了“未定义键";为视图模型创建视图时出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在按照"MVC音乐商店"教程创建一个项目,当为"ShoppingCartViewModel"创建视图时,它总是显示错误,提示我没有已定义键. 错误消息是:实体类型"ShoppingCartViewModel"没有定义键....

Currently I am creating a project following 'MVC Music Store' tutorial, when come to create a view for "ShoppingCartViewModel", it always shows the error that tells me that there is no Key definied. The error message is: Entity type'ShoppingCartViewModel' has no key defined....

这是教程中的原始代码:

Here is the original code from the tutorial:

namespace MyMVStore.ViewModels
{
     public class ShoppingCartViewModel
    {

       public List<Cart> CartItems { get; set; }
       public decimal? CartTotal { get; set; }
   }
}

这是我更新的内容:

//[NotMapping]
namespace MyMVStore.ViewModels
{
     public class ShoppingCartViewModel
    {
       [Key] //my code
       public int Id { get; set; } // my code
       public List<Cart> CartItems { get; set; }
       public decimal? CartTotal { get; set; }
   }
}

我将[Key]和Id放入模型中,似乎无法正常工作.我也尝试添加[Notmapping],它也不起作用.当我尝试为该模型创建视图时,仍然显示错误消息.

I put [Key] and Id to the model, seems not working. I also tried to add [Notmapping], it doesn't work either. The error massage still showing up when I tried to create the view for this model.

推荐答案

您需要做的就是从下拉列表中删除DataContext类.这是一个可编辑的下拉菜单,因此您只需突出显示并删除它即可.
完成后,效果很好.

All you need to do is remove the DataContext class from the dropdown. It's an editable dropdown, so you can just highlight it and delete it.
It works fine after you do that.

这篇关于“未定义键";为视图模型创建视图时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆