2015-02-10 57 views
0

我一直在挖掘其他帖子,试图找出如何从我的控制器中使用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”的新[位置]),然后可以从[位置]列表中选择要编辑的特定资产。

任何人都可以通过这个走过我吗?

很多建议是将SelectListIEnumerable<SelectListItem>添加到我的相关模型属性,但从那里我失去了什么在我的控制器/视图中调整?目前,我正在使用代码优先迁移,在我的DAL文件夹中为该项目提供InventoryTrackerContext.cs

回答

0

你混淆了这两个非常不同的方面。首先,Html.DropDownList只需要一个IEnumerable<SelectListItem>。传递完整的SelectList对象仅仅因为SelectListIEnumerable<SelectListItem>而满足该参数。不要使用SelectList的建议仅仅是为了让自己免于构建完整的SelectList对象(并且记住要将selectedValue设置为正确的项目),而Razor会为您处理这件事。无论您使用SelectList还是IEnumerable<SelectListItem>对您问题的其余部分都没有影响。

只要将项目添加到现有的下拉列表中,就必须使用JavaScript。在基本层面上,就像在DOM中选择select元素一样简单,并添加一个新的option节点。

+0

是不是可以通过控制器而不是JS/jQuery添加新选项? – 2015-02-10 20:42:35

+0

无需重新加载整个页面。由于用户通过弹出式菜单添加新选项,因此必须重新加载整个页面以刷新控制器的选项,或者使用JS动态插入新添加的选项。 – 2015-02-10 21:13:11