简体   繁体   中英

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

This is my model 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?)

What is wrong there?

Your User view, which you did not show entirely, seems to declare at the first line:

@model Moalem.UI.Web.Models.UserViewModel

You also didn't show this UserViewModel , but I guess it has no property named 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? What is the namespace of the UserViewModel you posted? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM