Asp.net MVC SelectList在文本字段中组合多列
如何生成一个选择列表,其中文本字段由两个或多个文本列组成,例如:如果我的数据库中有一个Description和Rate字段,我想将它们结合起来显示:Asp.net MVC SelectList在文本字段中组合多列,asp.net,asp.net-mvc,asp.net-mvc-3,razor,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,如何生成一个选择列表,其中文本字段由两个或多个文本列组成,例如:如果我的数据库中有一个Description和Rate字段,我想将它们结合起来显示: Large--£200 Medium--£150 Small--£100 控制器代码为: var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList(); ViewBag.StandID = new SelectList(stands,"StandID", "Descr
Large--£200
Medium--£150
Small--£100
控制器代码为:
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
ViewBag.StandID = new SelectList(stands,"StandID", "Description" + "-- £" + "Rate");
…我的观点是:
<div class="editor-field">
@Html.DropDownList("StandID", "--Select--")
</div>
…但描述为+-£+费率;不会运行:
数据绑定:
'系统.数据.实体.动态验证.支架_63F8C9F623B3C0E57D3008A57081AFCD9C39E1A6B79B0380B60840F1EFAE9DB4'
不包含名为“Description-£Rate”的属性
谢谢你的帮助
标记您可以使用简单的LINQ投影创建一个新的匿名类,然后使用SelectListIEnumerable、string、string指定要用于元素的值和文本字段,即:
var stands =
db.Stands
.Where(s => s.ExhibitorID == null)
.Select(s => new
{
StandID = s.StandID,
Description = string.Format("{0}-- £{1}", s.Description, s.Rate)
})
.ToList();
ViewBag.StandID = new SelectList(stands, "StandID", "Description")
编辑
在C6及更高版本中,比string.Format有更好的阅读效果
如果投影到强ViewModel类名而不是匿名类,则无疑需要用运算符的安全性替换神奇字符串:
ViewBag.StandID = new SelectList(stands, nameof(Stand.StandID), nameof(Stand.Description));
您正在使用的构造函数的格式是 选择ListIEnumerable items、string dataValueField、string dataTextField 因此,当您使用它时,实际上是在告诉它绑定到名为Description-£Rate的TextField,如果这不是DB中的字段名称,它将不知道您在指示什么 只要dataValueField中的值与放入值的属性名称匹配,并且dataTextField与放入文本的属性名称匹配,上述两种方法中的任何一种都可以工作,可能是上述两种解决方案的混合。只是因为我更喜欢lambda表达式而不是linq。并且使用selectlist项可以防止它在转换后必须对集合进行ToList。实际上,您正在创建自然绑定到选择列表的对象 在将描述或费率放入列表之前,您可能还需要检查它们是否为空
var stands = db.Stands.Where(s => s.ExhibitorID == null)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.Description + "-- £" + s.Rate.ToString()
});
ViewBag.StandID = new SelectList(stands, "Value", "Text");
可以创建局部模型类
public partial class Stand
{
public string DisplayName
{
get
{
return this.Description + "-- £" + this.Rate.ToString();
}
}
}
那么在你看来,
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
ViewBag.StandID = new SelectList(stands,"StandID", "DisplayName");
我通过修改视图模型实现了这一点,以下是我的代码: 视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcEsosNew.Models;
using System.Web.Mvc;
namespace MvcEsosNew.ViewModels
{
public class EntitlementViewModel
{
public int EntitlementCount { get; set; }
public Entitlement Entitlement { get; set; }
public SelectList Member { get; set; }
public SelectList Job_Grade { get; set; }
public SelectList Department { get; set; }
public SelectList Esos_Batch { get; set; }
}
public class department_FullName
{
public int deptID { get; set; }
public string deptCode { get; set; }
public string deptName { get; set; }
public string fullName { get { return deptCode + " - " + deptName; } }
}
}
控制器
public void getAllDepartment(EntitlementViewModel entitlementVM)
{
var department = from Department in db.Departments.Where(D => D.Status == "ACTIVE").ToList()
select new department_FullName
{
deptID = Department.id,
deptCode = Department.department_code,
deptName = Department.department_name
};
entitlementVM.Department = new SelectList(department, "deptID", "fullName");
}
景色
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.Entitlement.department_id)
</div>
<div class="col-sm-10">
@Html.DropDownListFor(model => model.Entitlement.department_id, Model.Department, new { @class="form-control" })
@Html.ValidationMessageFor(model => model.Entitlement.department_id)
</div>
</div>
结果是:
感谢您关于数据值匹配的注释。如果有人遇到LINQ函数问题,那么这个问题可能会有帮助,在匿名类型上使用nameof将需要额外的工作,特别是在枚举具有零元素的情况下。可能在这里有所帮助,特别是Marc的答案是:GetType.GetTypeInfo.GenericTypeArguments[0];在API连接的情况下,我们将如何做到这一点?我从中检索数据response.Content.ReadAsAsync.Result;得到了这段代码;。这是迄今为止对我最有帮助的答案!不需要摆弄可枚举项等等,只需使用partial对“main”类进行简单的扩展,就可以了
public void getAllDepartment(EntitlementViewModel entitlementVM)
{
var department = from Department in db.Departments.Where(D => D.Status == "ACTIVE").ToList()
select new department_FullName
{
deptID = Department.id,
deptCode = Department.department_code,
deptName = Department.department_name
};
entitlementVM.Department = new SelectList(department, "deptID", "fullName");
}
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.Entitlement.department_id)
</div>
<div class="col-sm-10">
@Html.DropDownListFor(model => model.Entitlement.department_id, Model.Department, new { @class="form-control" })
@Html.ValidationMessageFor(model => model.Entitlement.department_id)
</div>
</div>