2

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 eachOptionValue`.

Things that I am failing

  1. Accessing the ID of the OptionValue for which the SetVal needs to be populated.
  2. 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();
13
  • 2
    "the following viewmodel", "the following": did you forget to post the code? Commented Oct 7, 2015 at 11:43
  • yes.. posting it in a minute
    – Vini
    Commented Oct 7, 2015 at 11:43
  • @ViniVasundharan Can you elaborate, what build errors are you getting? It seems you provided incomplete code "op.OptionValues= " after the line if(op!=null){ ... Also please refine your questions with exact variable names you posted in code.
    – Siva Gopal
    Commented Oct 7, 2015 at 12:07
  • I did not write it because not even the Intellisense is suggesting me what I have in my code.
    – Vini
    Commented Oct 7, 2015 at 12:08
  • @SivaGopal: i have edited the code with other model classes and added more detailed explanation for my aim
    – Vini
    Commented Oct 7, 2015 at 12:23

1 Answer 1

1

If I understand your problem correctly, you are trying to assign a single object to a collection which is not allowed.

var setval = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == op.OptionID); //Notice FirstOrDefault(...)

As a quick(& of course dirty!) workaround, you may try with following:

viewModel.Values = new List<OptionValue_SetVal> {new OptionValue_SetVal{} };
viewModel.Values.First().SetValues = new List<SetValue> { setval };
//Assuming you have only one OptionValue_SetVal that hold setVal. Otherwise as a simple step you can go with foreach loop to make things work before you refactor.

NOTE: As a better approach, you can move these default collection initialization logic to a constructor within the class or a separate service as mentioned by @Jason.

Hope this help you...

23
  • I am not sure if i understood it right. you are trying to instantiate the list of OptionValue_SetValue and the the list of SetValue within the OptionValue_SetValue.
    – Vini
    Commented Oct 8, 2015 at 5:28
  • I ahve updted the question with the new controller and the error following.
    – Vini
    Commented Oct 8, 2015 at 5:41
  • @ViniVasundharan Based on your updated code & error, I see you are trying to assign a collection/list to a single object which is not allowed. So if you think you are going to get a collection of 'OptionValues' data then you need to hold a collection inside your viewmodel also or filter & assign only the required single 'OptionValue' object data you get from db.
    – Siva Gopal
    Commented Oct 8, 2015 at 6:52
  • Correct me if i am wrong : In my ViewModel ViewOptionValues , I have one Option and a collection of OptionValue_SetVal which in turn has a collection of SetValues. So should I be really using the second Viewmodel, because I already have the collection of SetVal in my OptionValue class.
    – Vini
    Commented Oct 8, 2015 at 7:01
  • So what I think I would get from the object of the ViewOptionValue ViewModel is : Option, A collection of OptionValues and a collection of SetValues. But I already have the collection of SetValues in my OptionValues. Am i making it redundant?
    – Vini
    Commented Oct 8, 2015 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.