在1个SelectList中组合2个实体框架模型字段



我正试图在SelectList中显示INV_Locations模型中的两个字段:location_dept|location_room或例如IT|Storage。使用这篇文章,我通过ViewData:将以下内容拼凑在一起

INV_AssetsController-Edit()获取:

    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.History = GetHistoryByAssetId(id);
        ViewData["Location_Id"] = new SelectList((from l in db.INV_Locations.ToList() select new { location_room = l.location_dept + "|" + l.location_room }), "location_room", null, null);
    }

INV_AssetsController-Edit()HttpPost:

    [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)
        {
            iNV_Assets.modified_date = DateTime.Now;
            iNV_Assets.modified_by = System.Environment.UserName;
            db.Entry(iNV_Assets).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index", "Home");
        }
        ViewData["Location_List"] = new SelectList((from l in db.INV_Locations.ToList() select new { location_room = l.location_dept + "|" + l.location_room }), "location_room", null, null);
        return View(iNV_Assets);
    }

INV_Assets-编辑()视图:

        <span class="control-label col-md-2">Location:</span>
        <div class="col-md-4">
            @*@Html.DropDownList("Location_Id", null, htmlAttributes: new { @class = "form-control dropdown" })*@
            @Html.DropDownListFor(model => model.Location_Id, (SelectList)ViewData["Location_List"], htmlAttributes: new { @class = "form-control dropdown", @id = "selectLocation" })
            @Html.ValidationMessageFor(model => model.Location_Id, "", new { @class = "text-danger" })
        </div>

这很接近,在我的下拉列表中呈现(例如)以下内容:

{ location_room = IT|Server }{ location_room = IT|Storage }

有人知道为了只在选择列表(IT|Server)中显示相关部分,我需要进行的语法更改吗?

您没有在SelectList构造函数中指定dataTextField属性,因此它默认为匿名对象的ToString()方法。它需要:(注意最后一个参数不是必需的)

ViewData["Location_List"] = new SelectList((from l in db.INV_Locations.ToList()
  select new { location_room = l.location_dept + "|" + l.location_room }),
  "location_room", "location_room");

旁注:

  1. 您的GET方法具有ViewData["Location_Id"](我认为这是打字错误,应该是ViewData["Location_List"](根据POST方法)
  2. 您尚未展示您的模型,但Location_Id建议identifier属性(通常为int),所以我不确定您会怎么做希望这能奏效。您正在绑定文本值"IT|Server"或"IT|Storage"到属性Location_Id,我怀疑该属性没有与模型或数据库字段的关系。我怀疑是什么您真正需要的是级联下拉列表,其中一个用于部门,第二个房间(开往Location_Id当选择一个部门时,它会使用ajax进行更新
  3. 我建议您重新考虑生成SelectList(以及其他公共代码)转换为私有方法以避免重复代码
  4. 我强烈建议你学会使用视图模型,不要混淆模型ViewBagViewData,并消除了[Bind(Include = "..")]属性

使用以下代码

    var ReportingManager = _context.EmployeeDetailMaster.Select(s => new {
                        RecID = s.RecId,
                        ReportingManagerName = s.FirstName + " " + s.MiddleName + " " + s.LastName
                    });
ViewData.ReportingManager = new SelectList(ReportingManager, "RecId", "ReportingManagerName");

最新更新