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 :
- Do I really require a ViewModel like
OptionValue_SetVal
? - 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
- Accessing the ID of the OptionValue for which the
SetVal
needs to be populated. - Populate
SetVal
This is my controller(It has build errors in the line adding values for SetVal)
public ActionResult ViewOptionvalues(int id)
{
var viewModel = new ViewOptionValues();
if (id != 0)
{
var op = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == id);
if(op!=null)
{
op.OptionValues=
var setval = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == op.OptionID);
viewModel.Values = setval.SetVal;
viewModel.OptionValues = op.OptionValues;
}
}
}
EDIT
Based on the comments I have removed the ViewModel ÒptionValue_SetValueand placed a collection of ÒptionValues
instead.
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); //set is being populated, but I am not sure what need to be placed instead of id in this line
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.
EDIT
Added join
var set = (from o in db.Option
join ov in db.OptionValue on o.OptionID equals ov.OptionID
join s in db.SetValue on ov.OptionValueID equals s.OptionValueID
where o.TechnicalCharacteristicID == s.TcSet.TechnicalCharacteristicID
select ov.SetValue).SingleOrDefault();