Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have the following ViewModel and I can't find a way to access the variables to perform a select operation

XML
 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

C#
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.

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
Posted
Updated 7-Oct-15 23:03pm
v7
Comments
John C Rayan 9-Oct-15 6:42am    
1. Do I really require a ViewModel like `OptionValue_SetVal` ?

You could have it because it simplifies your model for view.

2. How can I set the value for an object of SetVal from my controller?

var set = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == id).Select(x => new SetValue{SetValueID = x.SetValue.SetValueID,Value = x.SetValue.Value,Status=x.SetValue.Status}).AsEnumerable<setvalue>();
if(op!=null)
{
viewmodel.OptionValues = op.OptionValues;
viewModel.SetValues = set;

}
John C Rayan 12-Oct-15 7:54am    
Did you manage to do it?
vini vasundharan 13-Oct-15 4:51am    
Hello John. Sorry for such a delayed response. i could achieve it without using a ViewModel. May not be the best practice, but i could make it work. And i missed to convert the var set to list or Ienumerable i guess. and that prevented it from working

But finally without use of ViewModel i could do it. using ViewModel was making the model class redundant. I was getting messed up with all the One to many relationships.
vini vasundharan 13-Oct-15 4:53am    
And thanks a lot for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900