J  Smith
J Smith

Reputation: 35

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

Upvotes: 2

Views: 685

Answers (1)

user9405863
user9405863

Reputation: 1514

you need to access list in model like below

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

}

Upvotes: 3

Related Questions