MVC DisplayTemplate for Lookup list

466 Views Asked by At

I am calling a partial view on which I want to collapse a few dropdown controls(previously created by using DropDownListFor). Because the controls are readonly, I just need to show the selected value on each control. I have created a list called "salutations" in the controller, and pass it as ViewData to my partial view. On the partial view I need to see the selected salutation (e.g.. Mr/Miss/Dr)in my div using @Html.DisplayFor. I tried creating a DisplayTemplate according to an online posting, but I am still having issues getting this to work.

Lookup list declared like this in controller:

var salutations = (IEnumerable<lu_Salutation>)ViewData["salutations‌​"];

Here's my DisplayTemplate named LookupList.cshtml:

@model int
@using System.Linq
@vEmployee.SelectList1.Single(s => s.Value == Model.ToString()).Text

Of course, there's something wrong with the last line of the above code. vEmployee is the name of my model. How do I correct it?, and can I have a generic display template like the GridForeignKey Kendo EditorTemplate so I could easily pass the foreign key, the DisplayTemplate, and the lookup list to get just the text of the selected lookup value displayed?

Ideally, I will just like to have in my partial view, something like:

@Html.DisplayFor(model => model.id, "LookupList", SelectList((IEnumerable)ViewData["salutationList"], "TitleID", "Title"))

where TitleID and Title are respectively the value and text in the lookup list.

Models

public class lu_Salutation 
{ 
    public int TitleID { get; set; } // e.g. 1 
    public string Title { get; set; } // e.g. Mrs 
}

ViewModel Class - I want to use just IDs here, but display the matching Texts from the lookup tables (e.g lu_Salutation) when needed

public class vEmployee 
{ 
    [Key] 
    public int EmployeeID { get; set; } 
    public int SalutationID { get; set; } 
} 

Controller

[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; 
}
0

There are 0 best solutions below