我正在调用一个部分视图,我想要使用 DropDownListFor 创建一些下拉列表 controls(previously.因为控件是只读的,所以我只需要在每个控件上显示选定的值.我在控制器中创建了一个名为“salutations”的列表,并将其作为 ViewData 传递给我的局部视图.在局部视图中,我需要看到所选的称呼(e.g .. Mr/Miss/Dr)in 我的 div 使用@Html.DisplayFor。我尝试根据在线发布创建一个 DisplayTemplate,但我仍然遇到问题让这个工作。
查找列表在控制器中声明如下:
var salutations = (IEnumerable<lu_Salutation>)ViewData["salutations"];
这是我的名为 LookupList.cshtml 的 DisplayTemplate:
@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,但在需要时显示查找表(e.g 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;
}