在 1 个 SelectList 中合并 2 Entity Framework 个模型字段?

Combine 2 Entity Framework Model fields in 1 SelectList?

我正在尝试在 SelectList 中显示我的 INV_Locations 模型中的 2 个字段:location_dept|location_room 或例如 IT|Storage。使用 THIS post 我通过 ViewData:

拼凑了下面的内容

INV_AssetsController - 编辑() GET:

    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 - 编辑() 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 会建议 标识符 属性(通常是 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");