[英]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.