2015-09-06 59 views
1

我的下拉列表具有重複值爲item_brand。如何刪除重複項?嘗試使用不同的。基本上用戶必須選擇物品品牌,然後根據該品牌填充物品清單。但是,如果我有兩個產品相同的品牌,品牌名稱將在brandListDropDownList具有重複值

我的觀點

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

} 
+0

你可以有一個額外的屬性,如類別。 –

+0

您需要停止執行代碼轉儲並僅發佈相關代碼。幾乎所有這些都是不相關的,並且重要部分(ITEM的數據模型)缺失。您需要在查詢中使用'.Distinct()'並將其傳遞給比較器(請參閱[documentation](https://msdn.microsoft.com/en-us/library/vstudio/bb338049(v = vs.100) .aspx)舉例),但是我之前提到的一個問題中提到的基本問題是,您的數據庫結構是錯誤的。你需要一個單獨的表爲'品牌' –

回答

1

填充兩次由於您選擇的是ItemId ich我假設是獨一無二的,所以你有相同的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獨特的,你應該把它作爲價值)

,然後在獲取操作:

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

在model.brandList我得到這個錯誤:錯誤不能隱式轉換類型'System.Collections.Generic.List '到'System.Web .Mvc.SelectList' –

+0

好吧,我刪除了錯誤,但仍然獲得重複值 –