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