Home > other >  Binding List into View Model and Iterate through ir on View
Binding List into View Model and Iterate through ir on View

Time:01-25

I am doing MVC, I have view Models like this

  public class TaskViewModel
{
    public List<TaskProduct1> TaskProduct1{ get; set; };
    public List<TaskProduct2> TaskProduct2{ get; set; };
}

Now I want to access these view model in my view

I have used this line at the beginning of the view

 @model TotalTask.Models.TaskViewModel

but when I want to iterate through the list I can not access them

  @foreach (var item in Model) //Model.TaskProduct1 won't show Up here
    {
        <tr>
            <td>
                @item.Id
            </td>
            <td>
                @item.Subject
            </td>
           
            <td >
                @item.EndDate
            </td>
            
        </tr>
    } 

how can I iterate through List TaskProduct1 in view ???

CodePudding user response:

You can use

@foreach (var item in Model)
{
    <tr>
        <td>
            @item.TaskProduct1.Id
        </td>
        <td>
            @item.TaskProduct2.Subject
        </td>
       
        <td >
            @item.TaskProduct2.EndDate
        </td>
        
    </tr>
} 

CodePudding user response:

You should iterate as below: @foreach (var item in Model.TaskProduct1)

  •  Tags:  
  • Related