Reputation: 43
I have a commonViewModel which contains a List of other models. i am not able to refer the List of models which are in commonViewModel in my View through foreach, Really appreciate your help! Here is my View:
@model IEnumerable<MvcApplication1.ViewModels.Models.CommonViewModel>
<h2>All types of questions asked here</h2>
@foreach (var Mylist in Model.Question1)
{
<li>@Mylist.Question1</li>
}
commonViewModel class looks like below
public class CommonViewModel
{
public List<Question> Question1 {get; set;}
public List<MenuItem> MenuItem { get; set; }
}
Question Class looks like below:
public partial class Question
{
public int id { get; set; }
public string question1 { get; set; }
}
Error: Model.Question1 in View is not identified.
Error CS1061 'IEnumerable' does not contain a definition for 'Question1' and no extension method 'Question1' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Upvotes: 0
Views: 444
Reputation: 151672
Your view model is an IEnumerable<CommonViewModel>
, which in turn has a property Question1
.
So you can't directly iterate over Model.Question1
, because Model
is IEnumerable<CommonViewModel>
.
So you'll need to loop twice:
foreach (var viewModel in Model)
{
foreach (var question in viewModel.Question1)
{
<li>@question.WhatEver</li>
}
}
Upvotes: 2