将过滤器应用于下拉列表 [英] Apply filter to dropdown list
问题描述
我有以下课程,并希望根据特定条件过滤下拉列表。
模型类:
I have the following class and want to filter the dropdown list based on a certain condition.
Model Classes:
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; }
}
我想要实现的目标
我想在 SetVal
中保存每行的值。在 SetVal
中生成的下拉列表需要根据以下条件进行过滤(我无法实现条件,因为它使用来自其他类的参数)
OptionValue.Option.TechnicalCharacteristicID == TcSet.TCID
I有以下控制器类。我已将选择条件等同于预设值,因为我无法匹配选择条件的右侧。
我需要在表格中显示select的结果。当我尝试将现有结果解析为表时,它显示类型不是IEnumerable,即使它在模型类中声明为IEnumerable ..
控制器
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
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);
}
Create的参数来自Option Value控制器的函数调用。参数是 OptionValueID
和 OptionID
。
我不知道我是否有正确的方法来完成任务。
其他评论
即使使用预设值,列表也不起作用。没有编译或运行时错误
调试详情
我得到select语句中的两个值,但两个值都相同。它只对应于所需表格中行的第一个值。
有人可以帮我解决我做错的事吗?
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?
推荐答案
ViewBag.TcSetID = new SelectList(model.TcSet, "TcSetID", "SetName");
这篇关于将过滤器应用于下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!