i have the following ViewModel and I can't find a way to access the variables to perform a select operation
public class ViewOptionValues
{
public Option Option { get; set; }
public IEnumerable<OptionValue_SetVal> Values { get; set; }
}
public class OptionValue_SetVal
{
public OptionValue OptionValue { get; set; }
public IEnumerable<SetValue> SetValues { get; set; }
}
public class Option
{
public int OptionID { get; set;}
public string OptionName { get; set; }
public int LsystemID { get; set; }
public virtual ICollection<OptionValue> OptionValues { get; set; }
}
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; }
}
public class SetValue
{
public int SetValueID { get; set; }
public string Value { get; set; }
public bool Status { get; set; }
public int OptionValueID { get; set; }
public virtual OptionValue OptionValue { get; set; }
}
I have added the model class for Option to make it clear that it has a Collection of Option Values and my Option class has a collection of SetValues.
I have two questions :
1. Do I really require a ViewModel like `OptionValue_SetVal` ?
2. How can I set the value for an object of SetVal from my controller?
**What I am trying to achieve**
Select all OptionValues of a Parent class Option which i am able to achieve through the `var op`. Now through `var setVal` I am trying to populate the `IEnumerable<SetVal> for each `OptionValue`.
Things that I am failing
1. Accessing the ID of the OptionValue for which the `SetVal` needs to be populated.
2. Populate `SetVal`
Controller
public ActionResult ViewOptionValues(int id)
{
var viewmodel = new Option_OptionValues();
var op = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == id);
var set = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == id);
if(op!=null)
{
viewmodel.OptionValues = op.OptionValues;
}
return View(viewmodel);
}
View
@foreach (var item in Model.OptionValues)
{
<tr>
<td rowspan="@item.SetValue.Count">@item.OptionVal</td>
<td rowspan="@item.SetValue.Count">@item.OptionValueID</td>
@{var set = item.SetValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID);}
@if (set != null)
{
for (int i = 0; i < item.SetValue.Count; i++)
{
<tr>
<td>@set.TcSet.SetName</td>
<td> @set.Value</td>
<td>@set.Status</td>
</tr>
}
}
</tr>
}
In the view I am doing the controller logic(which is not right), but can't get different values for the SetValue.. The count is right but the values I get are the same.
I have eliminated the use of OptionValue_SetVal Viewmodel and eliminated the nested ViewModel.
Option_optionValue only has a Option and a collection of Option_Value