我正在调用一个局部视图,我想在该视图上折叠一些下拉控件(以前使用 DropDownListFor 创建)。因为控件是只读的,所以我只需要在每个控件上显示选定的值。我在控制器中创建了一个名为“salutations”的列表,并将其作为 ViewData 传递给我的局部视图。在部分视图中,我需要使用@Html.DisplayFor 在我的 div 中查看选定的称呼(例如 Mr/Miss/Dr)。我尝试根据在线帖子创建一个 DisplayTemplate,但我仍然无法使其正常工作。
在控制器中声明这样的查找列表:
var salutations = (IEnumerable<lu_Salutation>)ViewData["salutations"];
这是我的 DisplayTemplate,名为 LookupList.cshtml:
@model int
@using System.Linq
@vEmployee.SelectList1.Single(s => s.Value == Model.ToString()).Text
当然,上面代码的最后一行有问题。vEmployee 是我的模型的名称。我该如何更正它?我是否可以有一个像 GridForeignKey Kendo EditorTemplate 这样的通用显示模板,这样我就可以轻松地传递外键、DisplayTemplate 和查找列表来仅显示所选查找值的文本?
理想情况下,我只想在我的部分观点中拥有类似的东西:
@Html.DisplayFor(model => model.id, "LookupList", SelectList((IEnumerable)ViewData["salutationList"], "TitleID", "Title"))
其中 TitleID 和 Title 分别是查找列表中的值和文本。
楷模
public class lu_Salutation
{
public int TitleID { get; set; } // e.g. 1
public string Title { get; set; } // e.g. Mrs
}
ViewModel 类 - 我想在这里只使用 ID,但在需要时显示查找表中的匹配文本(例如 lu_Salutation)
public class vEmployee
{
[Key]
public int EmployeeID { get; set; }
public int SalutationID { get; set; }
}
控制器
[HttpGet]
public ActionResult EmployeeDetails(int employeeID)
{
vEmployee SelectedEmployee = GetEmployees(employeeID).First();
ViewData["salutations"] = _db.lu_Salutation.OrderBy(e => e.Title);
return PartialView("_EmployeeDetails", SelectedEmployee);
}
private IEnumerable<vEmployee>GetEmployees(int employeeID)
{
IEnumerable<vEmployee> emp = (from e in _db.Employees
join c in _db.Contacts on e.EmployeeID equals c.EmployeeID
join u in _db.lu_Salutation on c.SalutationID equals u.TitleID into sal
from u in sal.DefaultIfEmpty()
where (e.EmployeeID == employeeID))
select new vEmployee
{
EmployeeID = e.EmployeeID,
SalutationID = c.SalutationID
}).AsEnumerable().OrderBy(m => m.EmployeeNumber).ThenBy(m => m.FirstName);
return emp;
}