gpt4 book ai didi

c# - 林克 : Select multiple values and assign to select with specific option/value

转载 作者:行者123 更新时间:2023-11-30 22:58:26 24 4
gpt4 key购买 nike

我正在尝试选择多个值并将它们分配给具有特定选项/值的选择列表,然后通过 viewbag 将其传递到我的 View ,我遇到的问题是结构在呈现时和处于编辑模式时该值未预先填充。

为了达到这一点,我进行了各种搜索,但大多数使用 JQuery 或其他框架,我希望保留在 C#/MVC 中,只是我现在不确定要搜索什么短语才能找到现有的此类问题的解决方案。

谁能给我一些建议?

查看:

<div class="form-group">
@Html.LabelFor(model => model.employeeID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.employeeID, ViewBag.employee as List<SelectListItem>, "Select", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.employeeID, "", new { @class = "text-danger" })
</div>
</div>

Controller :

 var employeeID = ((from x in dbdev.employeeDetails orderby x.employeeID select new { Name =  x.employeeNumber + x.knownas + x.surname, Value = x.employeeID } ).Distinct()).ToList();
SelectList employeeIDList = new SelectList(employeeID);
ViewBag.employeeID = employeeIDList;

型号:

[Required]
[Range(1, 2147483647)]
[Display(Name = "Employee ID")]
public int employeeID { get; set; }

呈现我的代码后,选择具有以下结构

<option>{ Name = 568883PhilShort, Value = 1 }</option>

但实际上我正在尝试生成以下结构。

<option value="1">568883 Phil Short</option>

以下是根据评论修改后的

模型/HTML 未更改

根据评论更新 Controller :

 var employeesList = (from x in dbdev.employeeDetails.AsEnumerable()
orderby x.employeeID ascending
select new
{
Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
Value = x.employeeID
}).ToList();

SelectList employeeIDList = new SelectList(employeesList, "Value", "Name", userAccess.employeeID);
ViewBag.employeeID = employeeIDList;

然后为了减少我重构的代码如下:

  ViewBag.employeeID = new SelectList((from x in dbdev.employeeDetails.AsEnumerable()
orderby x.employeeID ascending
select new
{
Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
Value = x.employeeID
}).ToList(), "Value", "Name", userAccess.employeeID);

最佳答案

您只需要稍微修改一下,代码就会如下所示:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
.AsEnumerable()
.Select(x => new {
Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
Value = x.employeeID
}).ToList();

使用查询表达式的替代方法:

var employeesList = (from x in dbdev.employeeDetails.AsEnumerable()
orderby x.employeeID ascending
select new {
Name = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
Value = x.employeeID
}).ToList();

然后创建一个SelectList使用现有列表:

SelectList employeeIDList = new SelectList(employeesList, "Value", "Name", userAccess.employeeID);

或者... 只需创建一个 IEnumerable<SelectListItem>直接传递给DropDownListFor助手:

var employeesList = dbdev.employeeDetails.OrderBy(x => x.employeeID)
.AsEnumerable()
.Select(x => new SelectListItem {
Text = String.Format("{0} {1} {2}", x.employeeNumber, x.knownas, x.surname),
Value = x.employeeID
}).ToList();

关于c# - 林克 : Select multiple values and assign to select with specific option/value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com