应用过滤以生成下拉列表 [英] Apply filtering to generate a dropdown list

查看:15
本文介绍了应用过滤以生成下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程,想根据特定条件过滤下拉列表.

模型类

公共类选项{公共 int OptionID { 获取;放;}公共字符串 OptionName { 获取;放;}公共 int TechnicalCharacteristicID { 获取;放;}公共 int LsystemID { 获取;放;}公共虚拟 ICollectionOptionValues { 获取;放;}public virtual TechnicalCharacteristic TechnicalCharacteristic { get;放;}公共虚拟 Lsystem Lsystem { 获取;放;}}公共类 OptionValue{公共 int OptionValueID { 获取;放;}公共字符串 OptionVal { 获取;放;}公共 int OptionID { 获取;放;}公共虚拟选项选项{获取;放;}公共虚拟 ICollection<设置值>设置值{获取;放;}}公共类 SetValue{公共 int SetValueID { 获取;放;}公共字符串值 { 获取;放;}公共布尔状态{获取;放;}公共 int TcSetID { 获取;放;}公共 int OptionValueID { 获取;放;}公共虚拟 OptionValue OptionValue { 获取;放;}公共虚拟 TcSet TcSet { get;放;}}公共类 TcSet{公共 int TcSetID { 获取;放;}公共字符串 SetName { 获取;放;}[显示(名称=物理单位")]公共字符串 PhysicalUnit { 获取;放;}公共 int TechnicalCharacteristicID { 获取;放;}公共 int DataFormatID { 获取;放;}公共虚拟 ICollection设置值 { 获取;放;}公共虚拟数据格式数据格式 { 获取;放;}public virtual TechnicalCharacteristic TechnicalCharacteristic { get;放;}}公开课 TechnicalCharacteristic{公共 int TechnicalCharacteristicID { 获取;放;}公共字符串 TCName { 获取;放;}公共虚拟 ICollectionTcSets { 获取;放;}//公共虚拟ICollection<选项>选项 { 得到;放;}}

我想要达到的目标

我想在 SetVal 中保存每个选项的值.SetVal中生成的Dropdownlist需要根据以下条件进行过滤(我无法实现该条件,因为它使用了其他类的参数)

 OptionValue.Option.TechnicalCharacteristicID == TcSet.TCID

我有以下控制器类.我已将选择条件与预设值等同起来,因为我无法匹配选择条件的右侧.

我需要在表格中显示选择的结果.当我尝试将现有结果解析为表时,它显示该类型不是 IEnumerable,即使它在模型类中声明为 IEnumerable..

控制器

 public ActionResult Create(int OptionValID, int OptionID){var 模型 = 新的 SetValue{选项值 ID = 选项值 ID};//变量tcid =//ViewBag.OptionValueID = new SelectList(db.OptionValue, "OptionValueID", "OptionVal");var tcSet = db.SetValue.Include(x=>x.TcSet).FirstOrDefault(x=>x.OptionValue.Option.TechnicalCharacteristicID==4);如果 (tcSet!=null){模型.TcSet = tcSet.TcSet;}ViewBag.TcSetID = new SelectList(db.TcSet, "TcSetID", "SetName");返回视图(模型);}

Create 的参数来自 Option Value 控制器的函数调用.参数是 OptionValueIDOptionID.

我不知道我是否有正确的方法来完成任务.

其他评论

即使使用预设值,列表也不起作用.没有编译或运行时错误

调试详情

我从 select 语句中得到两个值,但是这两个值是相同的.它只对应于所需表中行的第一个值.

解决方案

使用以下代码生成你的选择列表数据

 public IEnumerable选择列表(){列表selectList = new List();var listOfCat1 = db.TcSets.ToList();如果(listOfCat1 != null){如果(listOfCat1.Count>0){foreach(listOfCat1 中的 var 项目){SelectListItem sVM = new SelectListItem();sVM.Value = item.Id.ToString();sVM.Text = item.Name;selectList.Add(sVM);}}}返回 selectList.AsEnumerable();}

然后,在调用视图之前将以下代码放入控制器中

 ViewBag.ProductCategoriesList = SelectList();

在您的视图中,使用以下代码行调用下拉列表

 

@Html.LabelFor(model => model.TcSetID , "Product Category", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.DropDownListFor(x => x.TcSetID , @ViewBag.ProductCategoriesList as IEnumerable, "--- Select Tc Set ---", new { @class = "form-control", @style = "背景色:黄色;边框色:皇家蓝" })@Html.ValidationMessageFor(model => model.TcSetID, "", new { @class = "text-danger" })

我希望这会有所帮助.

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; }
    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 option 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 tcid = 
       // ViewBag.OptionValueID = new SelectList(db.OptionValue, "OptionValueID", "OptionVal");
        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 Comments

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.

解决方案

Use the following code to generate your select list data

    public IEnumerable<SelectListItem> SelectList()
    {
        List<SelectListItem> selectList = new List<SelectListItem>();
        var listOfCat1 = db.TcSets.ToList();

        if (listOfCat1 != null)
        {
            if (listOfCat1.Count>0)
            {
                foreach (var item in listOfCat1)
                {
                    SelectListItem sVM = new SelectListItem();
                    sVM.Value = item.Id.ToString();
                    sVM.Text = item.Name;
                    selectList.Add(sVM);
                }
            }
        }

        return selectList.AsEnumerable();

    }

Then, put the following code in your controller before calling the view

    ViewBag.ProductCategoriesList = SelectList();

In your View, call the dropdown list with the following line of code

   <div class="form-group" >
      @Html.LabelFor(model => model.TcSetID , "Product Category", htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
       @Html.DropDownListFor(x => x.TcSetID , @ViewBag.ProductCategoriesList as IEnumerable<SelectListItem>, "--- Select Tc Set  ---", new { @class = "form-control", @style = "background-color:yellow;border-color:royalblue" })
          @Html.ValidationMessageFor(model => model.TcSetID , "", new { @class = "text-danger" })
      </div>
  </div>

I hope this helps.

这篇关于应用过滤以生成下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆