我一直在挖掘其他帖子,试图弄清楚如何在我的控制器中使用SelectList
来填充我的视图中的@Html.DropDownList
,而不是使用似乎是通常建议SelectListItem
,但我完全失去了?
我有一个主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>
我现在要做的是为每个列表添加一个值,并说明我要允许用户点击并选择(部分视图?)弹出的每个列表#34;添加新的&#34;他们立即添加一个新的相关记录(例如&#34;仓库2&#34;的新[位置]),然后可以从[位置]列表中选择要编辑的特定资产。
有人可以带我走过这个吗?
很多建议是在我的相关模型属性中添加SelectList
或IEnumerable<SelectListItem>
,但是从那里开始,我在控制器/视图中调整了什么?目前,我在项目的InventoryTrackerContext.cs
文件夹中使用了DAL
的代码优先迁移。
答案 0 :(得分:0)
你混淆了两个截然不同的方面。首先,Html.DropDownList
只需要IEnumerable<SelectListItem>
。传递完整的SelectList
对象仅仅因为SelectList
是IEnumerable<SelectListItem>
而满足此参数。不使用SelectList
的建议只是为了节省自己必须构建一个完整的SelectList
对象的工作(并记住做一些事情,比如将selectedValue
设置为正确的项目),剃刀会为你处理这个问题。您使用SelectList
还是IEnumerable<SelectListItem>
与问题的其余部分无关。
就在现有下拉列表中添加项目而言,您必须使用JavaScript。在基本级别,只需在DOM中选择select
元素并附加新的option
节点即可。