I have the following ViewModel and the controller to View the set in the ViewModel. I am not able to get the values from the nested collection in the ViewModel. But I can see the values are being populated in the right way in the parent class.
ViewModel
public class Option_OptionValues
{
public Option_OptionValues()
{
this.OptionValues = new List<OptionValue>();
}
public Option Option { get; set; }
public IEnumerable<OptionValue> OptionValues { get; set; }
}
Model Class OptionValue
public class OptionValue
{
public int OptionValueID { get; set; }
public string OptionVal { get; set; }
public int OptionID { get; set; }
public virtual Option Option { get; set; }
public virtual ICollection< SetValue> SetValue { get; set; }
}
Model Class Option
public class Option
{
public int OptionID { get; set;}
public string OptionName { get; set; }
public virtual ICollection<OptionValue> OptionValues { get; set; }
}
I have treid various methods to populate a list of SetValue by using join, but it did not work out. Now I have some of my controller logic in View.
Controller
public ActionResult ViewOptionValues(int id)
{
var viewmodel = new Option_OptionValues();
var op = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == id);
if(op!=null)
{
viewmodel.OptionValues = op.OptionValues;
}
return View(viewmodel);
}
I have not removed the comments so that I have all that I tried so far.
View
@model TEDALS_Ver01.ViewModels.Option_OptionValues
@{
ViewBag.Title = "ViewOptionsValues";
}
<div class="col-md-10">
<h2>View Options</h2>
<table class="table">
<tr>
<th>Option Name</th>
<th>Option value</th>
<th>Set Value</th>
<th></th>
</tr>
@foreach (var item in Model.OptionValues)
{
//var row = @item.SetValue.Count + 1;
@:<tr>
@:<td rowspan="@item.SetValue.Count">@item.OptionVal</td>
@:<td rowspan="@item.SetValue.Count">@item.OptionValueID</td>
var set = item.SetValue.Where(x => x.OptionValueID == item.OptionValueID).ToList();
//var set = item.SetValue.Where(x=>x.OptionValueID == item.OptionValueID);
if (set != null)
{
//for (int i = 0; i < item.SetValue.Count; i++)
int count = 0;
foreach(var i in set)
{
@item.SetValue.FirstOrDefault().TcSet.SetName
if (count == 0)
{
@:<td>@set.FirstOrDefault().TcSet.SetName</td>
@:<td> @set.FirstOrDefault().Value</td>
@:<td>@set.FirstOrDefault().Status</td>
@:</tr>
}
else
{
@:<tr>
@:<td>@set.FirstOrDefault().TcSet.SetName</td>
@:<td> @set.FirstOrDefault().Value</td>
@:<td>@set.FirstOrDefault().Status</td>
}
count++;
}
}
@:</tr>
}
</table>
</div>
I am only getting a single value for the SetValue though the setvalue has been populated properly in the variable Item. Is there a way I can access the