我一直在挖掘其他帖子,試圖找出如何從我的控制器中使用SelectList
填充我的視圖中的@Html.DropDownList
,而不是使用似乎是通常建議的SelectListItem
,但我完全失去了?如何正確使用SelectListItem代替SelectList的HTML.DropDownList?
我有一個主要的INV_Assets
模型,當我去編輯視圖,我包括其他模型屬性(位置,製造商,模型,狀態,類型,供應商等下拉列表)我的當前代碼充分填充列表並允許我在Edit()
上將所選實體值更改爲存儲在該相關表中的任何其他值。
當前代碼:
控制器:
// GET: INV_Assets/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
INV_Assets iNV_Assets = await db.INV_Assets.FindAsync(id);
if (iNV_Assets == null)
{
return HttpNotFound();
}
ViewBag.Location_Id = new SelectList(db.INV_Locations, "Id", "location_dept", iNV_Assets.Location_Id);
ViewBag.Manufacturer_Id = new SelectList(db.INV_Manufacturers, "Id", "manufacturer_description", iNV_Assets.Manufacturer_Id);
ViewBag.Model_Id = new SelectList(db.INV_Models, "Id", "model_description", iNV_Assets.Model_Id);
ViewBag.Status_Id = new SelectList(db.INV_Statuses, "Id", "status_description", iNV_Assets.Status_Id);
ViewBag.Type_Id = new SelectList(db.INV_Types, "Id", "type_description", iNV_Assets.Type_Id);
ViewBag.Vendor_Id = new SelectList(db.INV_Vendors, "Id", "vendor_name", iNV_Assets.Vendor_Id);
return View(iNV_Assets);
}
// POST: INV_Assets/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,Model_Id,Manufacturer_Id,Type_Id,Location_Id,Vendor_Id,Status_Id,ip_address,mac_address,note,owner,cost,po_number,description,invoice_number,serial_number,asset_tag_number,acquired_date,disposed_date,created_date,created_by,modified_date,modified_by")] INV_Assets iNV_Assets)
{
if (ModelState.IsValid)
{
db.Entry(iNV_Assets).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.Location_Id = new SelectList(db.INV_Locations, "Id", "location_dept", iNV_Assets.Location_Id);
ViewBag.Manufacturer_Id = new SelectList(db.INV_Manufacturers, "Id", "manufacturer_description", iNV_Assets.Manufacturer_Id);
ViewBag.Model_Id = new SelectList(db.INV_Models, "Id", "model_description", iNV_Assets.Model_Id);
ViewBag.Status_Id = new SelectList(db.INV_Statuses, "Id", "status_description", iNV_Assets.Status_Id);
ViewBag.Type_Id = new SelectList(db.INV_Types, "Id", "type_description", iNV_Assets.Type_Id);
ViewBag.Vendor_Id = new SelectList(db.INV_Vendors, "Id", "vendor_name", iNV_Assets.Vendor_Id);
return View(iNV_Assets);
}
查看 - 只需[位置]例如:
<div class="form-group">
@*@Html.LabelFor(model => model.Location_Id, "Location_Id", htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Location:</span>
<div class="col-md-10">
@Html.DropDownList("Location_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location_Id, "", new { @class = "text-danger" })
</div>
</div>
我試圖盡現是爲每個列表添加一個值,說明「添加新的」,哪個我想讓用戶點擊並有一個(局部視圖?)彈出窗口讓他們立即添加一個新的相關記錄(Ex。 「倉庫2」的新[位置]),然後可以從[位置]列表中選擇要編輯的特定資產。
任何人都可以通過這個走過我嗎?
很多建議是將SelectList
或IEnumerable<SelectListItem>
添加到我的相關模型屬性,但從那裏我失去了什麼在我的控制器/視圖中調整?目前,我正在使用代碼優先遷移,在我的DAL
文件夾中爲該項目提供InventoryTrackerContext.cs
。
是不是可以通過控制器而不是JS/jQuery添加新選項? – 2015-02-10 20:42:35
無需重新加載整個頁面。由於用戶通過彈出式菜單添加新選項,因此必須重新加載整個頁面以刷新控制器的選項,或者使用JS動態插入新添加的選項。 – 2015-02-10 21:13:11