Apply filtering to generate a dropdown list

跟風遠走 提交于 2019-12-18 05:23:18

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32962792/apply-filtering-to-generate-a-dropdown-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!