Click here to Skip to main content
15,885,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following class and want to filter the dropdown list based on a certain condition.

Model Classes:

C#
public class Option
{
    public int OptionID { get; set;}
    public string OptionName { get; set; }

    public int TechnicalCharacteristicID { get; set; }
    public int LsystemID { get; set; }

    public virtual ICollection<OptionValue> OptionValues { get; set; }
    public virtual TechnicalCharacteristic TechnicalCharacteristic { get; set; }
    public virtual Lsystem Lsystem { 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 TcSetID { get; set; }
    public int OptionValueID { get; set; }

    public virtual OptionValue OptionValue { get; set; }
    public virtual TcSet TcSet { get; set; }

}

public class TcSet
{
    public int TcSetID { get; set; }
    [Display (Name = "Property name")]
    public string SetName { get; set; }
    [Display(Name = "PhysicalUnit")]
    public string PhysicalUnit { get; set; }

    public int TechnicalCharacteristicID { get; set; }
    public int DataFormatID { get; set; }

    public virtual ICollection<SetValue> SetValues { get; set; }
    public virtual DataFormat DataFormat { get; set; }
    public virtual TechnicalCharacteristic TechnicalCharacteristic { get; set; }
}

public class TechnicalCharacteristic
{
    public int TechnicalCharacteristicID { get; set; }
    public string TCName { get; set; }

    public virtual ICollection<TcSet> TcSets { get; set; }
    //public virtual ICollection<Option> Options { get; set; }
}


What I am trying to achieve

I want to save values for each row in SetVal. The Dropdownlist generated in SetVal needs to filtered based on the following condition (I can't implement the condition as such as it uses parameters from other classes)

OptionValue.Option.TechnicalCharacteristicID == TcSet.TCID

I have the following controller class. I have equated the select condition with a preset value because I was unable to match the right hand side of the select condition.

I need to display the result of the select in a table. When i try to resolve the existing result to table it shows the type is not IEnumerable, even though it is declared IEnumerable in the model class..

Controller

C#
public ActionResult Create(int OptionValID, int OptionID)
{
    var model = new SetValue
    {
        OptionValueID = OptionValID
    };
    var tcSet = db.SetValue.Include(x=>x.TcSet).FirstOrDefault(x=>x.OptionValue.Option.TechnicalCharacteristicID==4);
    if (tcSet!=null)
    {
        model.TcSet = tcSet.TcSet;
    }
    ViewBag.TcSetID = new SelectList(db.TcSet, "TcSetID", "SetName");
    return View(model);
}

The parameters for Create come from a function call from the Option Value controller. The parameters are the OptionValueID and OptionID.

I don't know if I have the right approach for achieving the task.


Additional Comment

Even with the preset value the list doesn't work. There are no compile or runtime errors

Debug details

I get two values from the select statement, but both the values are the same. It only corresponds to the first value of the row from the required table.

Can somebody help me with what I am doing wrong?
Posted
Updated 5-Oct-15 21:45pm
v3
Comments
John C Rayan 6-Oct-15 8:23am    
where is db.SetValue? Can you show some data too?





I am not quite sure what you are trying to do here?


You want to compare TechnicalCharacteristicID in SetValue then why do you get TcSet?

db.SetValue.Include(x=>x.TcSet).FirstOrDefault(x=>x.OptionValue.Option.TechnicalCharacteristicID==4);


vini vasundharan 8-Oct-15 4:04am    
i have already added all the model classes that are referred in the context.
So as you can see in my model classes, I have Option(each with a TechnicalCharacteristic) with many OptionValues.
Each OptionValue contains a collection of SetValue which holds the value for one property of TcSet associated with a TechnicalCharacteristic.

SideNote : TechnicalCharacteristic is a collection of TcSet.

A real example could be the following with sample data.

Technical Characterstic : WheelCup
TcSet for WheelCup(for simplicity only the TCSet Name is mentioned) : Diameter, material, Durability
Option : Wheel (Foreign Key : WheelCup)
OptionValues for Wheel : 01,02,03
What I want to achieve

for Option Value 01, I need to insert values for TcSet with foreign key WheelCup

So i have SetValue

SetValue for OptionValue 01
Diameter :6
Material : Plastic
durability: Long

SetValue for OptionValue 02
Diameter : 9
Material : Steel
Durability : long

I want to list in a dropdown only the TcsetName where the TechnicalCharacteristic matches with the Option.

Hope I could make it clear.
John C Rayan 8-Oct-15 6:57am    
You have to do something similar to this. Consider using join if it doesn't give you all values from other tables. Note that I haven't tested it so it has to be considered as pseudo code:

var tcSet = db.SetValue.Include(x=>x.TcSet)
.Where(x=>x.OptionValueId==OptionValID)
.Where(x=>x.optionValue.option.TechnicalCharacteristicID == x.TcSet.TechnicalCharacteristicID)
.Select(x.TcSet.SetName);

I think your structure so complex , I would break them into nicely for better query.

vini vasundharan 8-Oct-15 8:35am    
I have tried a lot to make the model more simpler. But i think when i tried making it simple it got more complex. making it even difficult make a delete function work. I will try the join.
I already tried join once for another class and it never worked out. i am not really well versed in using joins. But i will try.

1 solution

C#
ViewBag.TcSetID = new SelectList(model.TcSet, "TcSetID", "SetName");
 
Share this answer
 
Comments
vini vasundharan 8-Oct-15 4:05am    
It will select all the elements in TcSet, but i only want the elements where a particular condition is satisfied

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