Search code examples
c#asp.net-mvcrazor-2

razor foreach in model can not be used


Good day,

I have this viewmodel

public class ProjectTypeViewModel
{      
    public string CategoryName{ get; set; }
    public IEnumerable<ProjectType> Tipo { get; set; }
}
public class ProjectType
{
    [Required]
    public Guid Id { get; set; }
    [MaxLength(512)]
    [Required]
    public string Type{ get; set; }
    [Required]
    public string AddedBy { get; set; }
    public Category Category{ get; set; }
    public Guid? CategoryId{ get; set; }

}

I want to return thae ProjectTypeViewModel into my view

@model  List<MyProject.ViewModels.ProjectTypeViewModel>
@foreach(var tipo in Model )
{

}

I have a problem here, my model displays the following error:

foreach statement can not operate in variables of type ProjectTypeViewModel because does not contain a definition for GetEnumerator.

What am I doing wrong?

Thanks


Solution

  • you need to access list in model like below

    @model  MyProject.ViewModels.ProjectTypeViewModel
    @foreach(var tipo in Model.Tipo)
    {
    
    }