简体   繁体   English

为什么当我告诉我模型属性(id)不存在时?

[英]Why am i being told my model property (id) doesn't exist, when it does?

I have a model property RegimeItemID which I am using in my controller. 我有一个在控制器中使用的模型属性RegimeItemID However, when I am trying to call it, it is giving an error that it doesnt exist. 但是,当我尝试调用它时,它给出了一个不存在的错误。 What have I done wrong? 我做错了什么?

Controller 控制者

public ActionResult ExerciseIndex(int? id, UserExerciseViewModel vmodel)
        {
            User user = db.Users.Find(id);
            //user.RegimeItems = ChosenExercises();

            UserExerciseViewModel model = new UserExerciseViewModel { AvailableExercises = GetAllExercises(), RequestedExercises = ChosenExercises(user, vmodel) };
            //user.RegimeItems = db.RegimeItems.Find(model.SelectedExercise);
            user.RegimeItems = model.RequestedExercises;
            return View(model);
        }

private List<RegimeItem> ChosenExercises(User user, UserExerciseViewModel model)//RegimeItem regimeItem)//User user, RegimeItem regimeItem)
        {
            return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID).ToList();
        }

Models 楷模

 public class User
    {
        public int UserID { get; set; }
        public ICollection<RegimeItem> RegimeItems { get; set; }
        public User()
        {
            this.RegimeItems = new List<RegimeItem>();
        } 
    }
    public class RegimeItem
    {
        public int RegimeItemID { get; set; }
        public Exercise RegimeExercise { get; set; }
    }

ViewModel 视图模型

public class UserExerciseViewModel
{
    public List<Exercise> AvailableExercises { get; set; }
    public List<RegimeItem> RequestedExercises { get; set; }
    public int? SelectedExercise { get; set; }
    public int[] AvailableSelected { get; set; }
    public int[] RequestedSelected { get; set; }
    public string SavedRequested { get; set; }
}

I am getting the error: 我收到错误消息:

'System.Collections.Generic.ICollection<Project.Model.RegimeItem>' does not contain a definition for 'RegimeItemID' and no extension method 'RegimeItemID' accepting a first argument of type 'System.Collections.Generic.ICollection<Project.Model.RegimeItem>' could be found (are you missing a using directive or an assembly reference?)

I am getting it on this line in the controller: 我在控制器的这一行上得到它:

return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID).ToList();

RegimeItems is a collection of RegimeItem elements - it's not a single element, so it doesn't have a single ID. RegimeItemsRegimeItem元素的集合 -它不是单个元素,因此没有单个ID。

It sounds like you may want something like: 听起来您可能想要以下内容:

return db.Users
         .Where(r => r.RegimeItems.Any(ri => ri.RegimeItemID == user.UserID))
         .ToList();

That will find users who have any regime items with a regime item ID equal to their user ID. 这将查找具有任何方案项目ID等于其用户ID的方案项目的用户。 It's not really clear whether that's a sensible query though - why would a regime item ID be equal to a user ID? 不过,目前还不清楚这是否是一个明智的查询-为何制度项ID等于用户ID? It also doesn't have the right return type - that's going to return a list of User s, not RegimeItem s. 它也没有正确的返回类型-这将返回User列表,而不是RegimeItem列表。

It seems more likely that you want something like: 您似乎更想要以下内容:

return db.Users
         .Where(u => u.UserID == user.UserID)
         .SelectMany(u => u.RegimeItems)
         .ToList();

Or possibly: 或可能:

return db.Users
         .Where(u => u.UserID == user.UserID)
         .Single()
         .RegimeItems
         .ToList();

(You should look at the queries involved in each case.) (您应该查看每种情况下涉及的查询。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我被告知这种类型转换是多余的? - Why am I being told this typecast is redundant? 在定义变量后,为什么会显示错误消息指示变量不存在? - Why am I getting an error indicating that my variable doesn't exist, when I've already defined it? 当每个被引用的控件必须存在并且具有Text属性时,为什么会得到NRE? - Why am I getting an NRE when every control being referenced must exist and has a Text property? 剃刀视图声明属性在模型上不存在 - Razor view claims property doesn't exist while it does on model 为什么 Visual Studio 说我的文件显然不存在? 另外,添加正斜杠是否重要? - Why is Visual Studio saying my file doesn't exist when it evidently does? Also, does it matter that it's adding a forward slash? 为什么我从我的 C# 代码中得到“名称 Regex 在当前上下文中不存在”? - Why am I getting “The name Regex does not exist in the current context” from my C# code? 为什么我的UpdateAsync(用户)方法第一次失败,声称用户不存在? - Why does my UpdateAsync(user) method fail the first time, claiming user doesn't exist? 为什么我的id和name属性并不总是与模型属性相同? - Why are my id and name attributes not always the same as my model property? 当我告诉它时,为什么这段代码缓冲TCP输出? - Why Is This Code Buffering TCP Output When I Told it Not To? 为什么我的数据库上下文中不存在ObjectStateManager属性? - Why does the ObjectStateManager property not exist in my db context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM