Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
public class Option_OptionValues
    {
        public Option_OptionValues()
        {
           // this.Option = new Option();
            this.OptionValues = new List<OptionValue>();
            //this.OptionValues.First().SetValue = new List<SetValue>();
        }
        public Option Option { get; set; }
        public IEnumerable<OptionValue> OptionValues { get; set; }
    }

Model Class OptionValue

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

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 = (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).ToList();

           //var set = db.OptionValue.Include(x => x.SetValue).Where(x => x.OptionValueID == op.OptionValues.FirstOrDefault().OptionValueID).ToList();
           //for(int i=0; i< op.OptionValues.Count ;i++)
           //{
           //    var set = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionID == op.OptionID);
           //}

           //var set = db.OptionValue.Include(x => x.SetValue).FirstOrDefault(x => x.OptionValueID == id);

           if(op!=null)
           {
               viewmodel.OptionValues = op.OptionValues;
              // viewmodel.OptionValues.FirstOrDefault().SetValue = op.OptionValues.FirstOrDefault().SetValue.Where(x => x.OptionValueID == id).ToList();
               //viewmodel.OptionValues.First().SetValue = new List<SetValue> {  };
           }
           return View(viewmodel);
       }


I have not removed the comments so that I have all that I tried so far.

View

HTML
@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
Posted
Updated 10-Oct-15 12:11pm
v4

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