DropDownList具有重复值

时间:2015-09-06 18:45:27

标签: asp.net asp.net-mvc

我的下拉列表中有重复值为item_brand。如何删除重复项?尝试过使用不同的。基本上,用户必须选择商品品牌,然后根据该品牌填充商品列表。但如果我有两个品牌相同的产品,品牌名称将在brandList中填充两次

我的观点

   @using (Html.BeginForm(new { OrderID = Model.OrderID }))
   {

        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>Item Information</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })


            <section class="panel">


                <div class="panel-body">
                    <div class="form-group">
                        @Html.LabelFor(m => m.SelectedBrand, "Brand", htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.DropDownListFor(m => m.SelectedBrand,Model.BrandList, htmlAttributes: new { @class = "form-control" })

                            @Html.ValidationMessageFor(m=>m.SelectedBrand, "", new { @class = "text-danger" })


                            </div>
                    </div>



                    <div class="form-group">
                        @Html.LabelFor(m => m.SelectedItem, "Description", htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.DropDownListFor(m => m.SelectedItem, Model.ItemList, htmlAttributes: new { @class = "form-control" })

                            @Html.ValidationMessageFor(m => m.SelectedItem, "", new { @class = "text-danger" })


                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.item_order_quantity, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.item_order_quantity, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.item_order_quantity, "", new { @class = "text-danger" })
                        </div>
                    </div>


                <div class="form-group">
                    @Html.HiddenFor(model => model.OrderID, htmlAttributes: new { @class = "control-label col-md-2" })

                    <div class="col-md-10">
                        @Html.HiddenFor(model => Model.OrderID, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.OrderID, "", new { @class = "text-danger" })
                    </div>
                </div>


                    </div>
            </section>
        </div>


                <section class="panel">

                    <div class="panel-body">

                        <input type="submit" value="Add Item" class="btn btn-default" style="float:right;" />
                        <a href="@Url.Action("Details", "Order", new { id = Model.OrderID }, null)" class="btn btn-info"> Back</a>

                    </div>
                </section>



    <!-- JS includes -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>

    <script type="text/javascript">
            var itemUrl = '@Url.Action("FetchItems")';
            var items = $('#SelectedItem');

            $('#SelectedBrand').change(function() {
                items.empty();

                $.getJSON(itemUrl, { brand: $(this).val()},function(data) {
                    if (!data) {
                        return ;
                    }
                    items.append($('<option></option>').val('').text('Please select'));

                    $.each(data, function(index, item) {
                        items.append($('<option></option>').val(item.Value).text(item.Text));
                    });
                });

            })


    </script>
    }
</body>
</html>

控制器

// GET: ItemOrder/Create
    public ActionResult Create(int ID)
    {
        ORDER order = db.Order.Find(ID);
        ItemOrderVM model = new ItemOrderVM() { 
        OrderID = order.OrderID
        };

        ConfigureViewModel(model);
        return View(model);




    }
    [HttpGet]
    public JsonResult FetchItems(int brand)
    {
        var data = db.Item.Where(l => l.ItemID == (brand))
            .Select(l => new { Value = l.ItemID, Text = l.item_description });

        return Json(data, JsonRequestBehavior.AllowGet);
    }
    private void ConfigureViewModel(ItemOrderVM model)
    {
        var brand = (from m in db.Item
                        select m);

        model.BrandList = new SelectList(db.Item, "ItemID", "item_brand");

       if (model.SelectedBrand.HasValue)
        {
            IEnumerable<ITEM> items = db.Item.Where(l => l.item_brand.Equals(model.SelectedBrand));
            model.ItemList = new SelectList(items, "ItemID", "item_description");
        }
        else
        {
            model.ItemList = new SelectList(Enumerable.Empty<SelectListItem>());
        }

    }

ItemOrder查看模型

public class ItemOrderVM
{
    public int? ID { get; set; }
    public int OrderID { get; set; }
    public int ItemID { get; set; }
    [DisplayName("Quantity")]
    [Range(1, int.MaxValue, ErrorMessage = "Quantity must be greater than 0")]
    public int item_order_quantity { get; set; }
    [Display(Name = "Brand")]
    public int ? SelectedBrand { get; set; }
     [Display(Name = "Description")]
    public int SelectedItem { get; set; }
     public SelectList BrandList { get; set; }
     public SelectList ItemList { get; set; }
     public List<OrderVM> Orders { get; set; }

}

1 个答案:

答案 0 :(得分:1)

您的问题出现在model.BrandList,因为您选择了我认为唯一的ItemId,因此您有多次出现的item_brand

所以你需要的只是获得不同品牌的清单。 在ConfigureViewModel方法中:

model.BrandList = db.Item.Select(i => new SelectListItem{Text = i.item_brand, Value = i.item_brand}).Distrinct().ToList();

(如果你有一个item_brand_id属性,对于每个brend都是唯一的,你应该将它作为值使用)

然后在你的Fetch动作中:

[HttpGet]
public JsonResult FetchItems(string brand)
{
    var data = db.Item.Where(l => l.item_brand == brand)
        .Select(l => new { Value = l.ItemID, Text = l.item_description });

    return Json(data, JsonRequestBehavior.AllowGet);
}
相关问题