简体   繁体   English

剃刀视图声明属性在模型上不存在

[英]Razor view claims property doesn't exist while it does on model

This is my model UserViewModel : 这是我的模型UserViewModel

public class UserViewModel
{
    public int UserId { get; set; }
    public int GroupId { get; set; }
    public virtual Group Group { get; set; }
    public string Email { get; set; }
    public virtual List<Favorite> Favorites { get; set; }
}

I wrote these lines in my User view: 我在用户视图中写了以下几行:

@foreach (var lessonItem in Model.Group.Lessons)
{
    foreach (var favoriteItem in Model.Favorites)
    {
    }
}

And the result is this: 结果是这样的:

Compiler Error Message: CS1061: ' Moalem.UI.Web.Models.UserViewModel ' does not contain a definition for ' Favorites ' and no extension method ' Favorites ' accepting a first argument of type ' Moalem.UI.Web.Models.UserViewModel ' could be found (are you missing a using directive or an assembly reference?) 编译器错误消息:CS1061:“ Moalem.UI.Web.Models.UserViewModel ”不包含“ Favorites ”的定义,也没有扩展方法“ Favorites ”接受类型为“ Moalem.UI.Web.Models.UserViewModel ”的第一个参数可以找到(您是否缺少using指令或程序集引用?)

What is wrong there? 那里怎么了

Your User view, which you did not show entirely, seems to declare at the first line: 您的User视图(未完全显示)似乎在第一行声明:

@model Moalem.UI.Web.Models.UserViewModel

You also didn't show this UserViewModel , but I guess it has no property named Favorites . 您也没有显示此UserViewModel ,但我想它没有名为Favorites属性。

If you show all relevant code, perhaps more useful answers can be given. 如果显示所有相关代码,则可能会给出更有用的答案。


Further questions according to your edit: what does the @model on top of your view say? 根据您的修改,还有其他问题:视图顶部的@model表示什么? What is the namespace of the UserViewModel you posted? 您发布的UserViewModel的名称空间是什么? Are there any other errors, does the project containing the model compile successfully? 还有其他错误,包含模型的项目是否可以成功编译? Unless you're doing something really really special (which you aren't), the compiler is always right. 除非您所做的事情确实非常特别(不是),否则编译器始终是正确的。 The problem is in your code. 问题出在您的代码中。

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

相关问题 C# 声称我的 SQLite 表不存在,但确实存在 - C# claims my SQLite table doesn't exist but it does 远程服务器声明表不存在 - Remote server claims table doesn't exist 为什么当我告诉我模型属性(id)不存在时? - Why am i being told my model property (id) doesn't exist, when it does? Razor的当前上下文中不存在名称“模型” - The name 'Model' does not exist in the current context in Razor 模型属性未传递到Razor View - Model property not being passed to Razor View 在MVC视图中使用Razor循环模型属性 - Loop through Model property with Razor in MVC View 由razor视图模型调用时,不传递(int)枚举到对象属性 - Casting (int)enum to a object property does not get passed when called by razor view model 添加到视图模型的属性不会在组合框中生成项目 - Property added to view model doesn't produce items to a combo box ASP MVC Razor View 不想显示模型中的 @Html.TextBoxFor 值 - ASP MVC Razor View doesn't want to show @Html.TextBoxFor value from the model 如果不存在则插入导航属性,或者如果不存在则插入导航属性 - Insert navigation property if it doesn't exist or just use it if it does exist without key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM