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

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

问题描述

我有下面的类并希望根据一定的条件来筛选下拉列表。

I have the following class and want to filter the dropdown list based on a certain condition.

模型类

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; }
}

我想实现

我要救值在 SETVAL 每个选项。将DropDownList在生成的 SETVAL 需要过滤,基于以下条件(我无法实现的条件是这样的,因为它使用从其他类参数)

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

我有以下的控制器类。我已经等同于一个preset的价值选择条件,因为我无法的选择条件右侧匹配。

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.

我需要在一个表中显示选择的结果。当我尝试将现有的解决结果表它显示的类型不是IEnumerable的,即使它被宣布在模型类..

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

控制器

    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);
    }

有关创建来自从选项值控制函数调用的参数。该参数是 OptionValueID OptionID

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.

附加评论

即使在preSET值列表不起作用。有没有编译或运行时错误

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

调试详情

我从select语句两个值,但两者的值是相同的。它仅对应于行的从所需的表的第一个值。

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.

推荐答案

使用以下code来生成你的选择列表中的数据

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();

    }

然后,把下面的code在控制器调用视图前

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

    ViewBag.ProductCategoriesList = SelectList();

在您查看,调用下拉列表与下面的行code的

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天全站免登陆