Asp.net DropDownList具有重复的值

Asp.net DropDownList具有重复的值,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我的dropdownlist具有重复的值,即item_品牌。如何删除重复项?我试过使用distinct。基本上,用户必须选择项目品牌,然后根据该品牌填充项目列表。但是,如果我有两个相同品牌的产品,那么这个品牌名称将在品牌列表中出现两次 我的看法 @using (Html.BeginForm(new { OrderID = Model.OrderID })) { @Html.AntiForgeryToken() <div class="form

我的dropdownlist具有重复的值,即item_品牌。如何删除重复项?我试过使用distinct。基本上,用户必须选择项目品牌,然后根据该品牌填充项目列表。但是,如果我有两个相同品牌的产品,那么这个品牌名称将在品牌列表中出现两次

我的看法

   @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>
@使用(Html.BeginForm(新的{OrderID=Model.OrderID}))
{
@Html.AntiForgeryToken()
项目信息

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(m=>m.SelectedBrand,“Brand”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownListFor(m=>m.SelectedBrand,Model.BrandList,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(m=>m.SelectedBrand,“,new{@class=“text danger”}) @LabelFor(m=>m.SelectedItem,“Description”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownListFor(m=>m.SelectedItem,Model.ItemList,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(m=>m.SelectedItem,“,new{@class=“text danger”}) @LabelFor(model=>model.item_order_quantity,htmlAttributes:new{@class=“control label col-md-2”}) @EditorFor(model=>model.item_order_quantity,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.item_order_quantity,“,new{@class=“text danger”}) @HiddenFor(model=>model.OrderID,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.HiddenFor(model=>model.OrderID,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.OrderID,“,new{@class=“text danger”}) var itemUrl='@Url.Action(“FetchItems”); 变量项=$('SelectedItem'); $('#SelectedBrand')。更改(函数(){ items.empty(); $.getJSON(itemUrl,{brand:$(this).val()},函数(数据){ 如果(!数据){ 返回; } items.append($('').val('').text(''请选择'); $。每个(数据、功能(索引、项目){ items.append($('').val(item.Value).text(item.text)); }); }); }) }
控制器

// 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/Create
公共操作结果创建(int ID)
{
ORDER-ORDER=db.ORDER.Find(ID);
ItemOrderVM模型=新的ItemOrderVM(){
OrderID=order.OrderID
};
配置视图模型(模型);
返回视图(模型);
}
[HttpGet]
公共JsonResult获取项目(int品牌)
{
var data=db.Item.Where(l=>l.ItemID==(品牌))
.Select(l=>new{Value=l.ItemID,Text=l.item\u description});
返回Json(数据,JsonRequestBehavior.AllowGet);
}
私有void配置视图模型(ItemOrderVM模型)
{
var brand=(从数据库项目中的m开始)
选择m);
model.BrandList=新选择列表(db.Item,“ItemID”,“Item_brand”);
if(model.SelectedBrand.HasValue)
{
IEnumerable items=db.Item.Where(l=>l.Item_brand.Equals(model.SelectedBrand));
model.ItemList=新选择列表(项目,“项目ID”、“项目描述”);
}
其他的
{
model.ItemList=新建SelectList(Enumerable.Empty());
}
}
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; }

}
公共类ItemOrderVM
{
公共int?ID{get;set;}
公共int-OrderID{get;set;}
公共int ItemID{get;set;}
[显示名称(“数量”)]
[范围(1,int.MaxValue,ErrorMessage=“数量必须大于0”)]
公共整数项\顺序\数量{get;set;}
[显示(名称=“品牌”)]
public int?SelectedBrand{get;set;}
[显示(Name=“Description”)]
public int SelectedItem{get;set;}
public SelectList BrandList{get;set;}
public SelectList ItemList{get;set;}
公共列表顺序{get;set;}
}

您的问题在于
型号。品牌列表
,因为您选择的是
ItemId
,我假设它是唯一的,所以您有相同的
品牌
,多次出现

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

model.BrandList = db.Item.Select(i => new SelectListItem{Text = i.item_brand, Value = i.item_brand}).Distrinct().ToList();
(如果您有一个
item\u brand\u id
属性,该属性对于每个brend都是唯一的,您应该将其用作值)

然后在获取操作中:

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

你可以有一个额外的属性,比如Category。你需要停止代码转储,只发布相关的代码。几乎所有这些都是不相关的,并且缺少重要的部分(《代码》项的数据模型)。您需要在查询中使用
.Distinct()
,并向其传递一个比较器(请参阅示例),但正如我在前面的一个问题中指出的,您的根本问题是数据库结构错误。你需要一张单独的桌子