我的下拉列表有重复的item_brand值。如何删除重复项?我试过用distinct。基本上,用户必须选择商品品牌,然后将根据该品牌填充商品列表。但如果我有两个相同品牌的产品那么品牌名称会在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 View Model
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; }
}
您的问题是在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
属性,每个品牌都是唯一的,你应该使用它作为值)
然后在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);
}