'Binding List into View Model and Iterate through ir on View
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 ???
Solution 1:[1]
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>
}
Solution 2:[2]
You should changed your iterator to be on Model.TaskProduct1 as follows: @foreach (var item in Model.TaskProduct1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Düzgün Emre Özkan |
| Solution 2 |
