Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c# - Apply filtering to generate a 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; }
    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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...