这是我的控制人:
var repo = new CustomerRepository();
var customerList = repo.GetTemplates();
SendTemplateModel mod = new SendTemplateModel();
mod.Templates = customerList;
return View(mod);
这是下拉视图:
@Html.DropDownListFor(x => Model.SelectedTemplate, new SelectList(Model.Templates, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "template"})
这是GetList类
public IEnumerable<SelectListItem> GetTemplates()
{
using (MailsEntities5 context = new MailsEntities5())
{
List<SelectListItem> countries = context.templateMails.AsNoTracking()
.OrderBy(n => n.name)
.Select(n =>
new SelectListItem
{
Value = n.Id.ToString(),
Text = n.name
}).ToList();
var countrytip = new SelectListItem()
{
Value = null,
Text = "--- Select templates ---"
};
countries.Insert(0, countrytip);
return new SelectList(countries, "Value", "Text");
}
}
但是我只能得到“文本”,它是模板的名称。但是,我想获取ID(即“值”),以便从实体获取数据。请帮忙!
答案 0 :(得分:0)
我解决了自己
public IEnumerable<SelectListItem> GetTemplates()
{
using (MailsEntities5 context = new MailsEntities5())
{
List<SelectListItem> countries = context.templateMails.AsNoTracking()
.OrderBy(n => n.Id)
.Select(n =>
new SelectListItem
{
Value = n.Id.ToString(),
Text = n.name
}).ToList();
var countrytip = new SelectListItem()
{
Value = null,
Text = "--- Select templates ---"
};
countries.Insert(0, countrytip);
return new SelectList(countries, "Value", "Text");
}
}
更改为n.Id