0

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; 
}
user938455
  • 89
  • 11
  • You have not shown what your model is or what `lu_Salutation` is, but I assume you want `@salutations.Single(s => s.Value == Model.ToString()).Text` –  Nov 11 '17 at 22:07
  • The model is vEmployee which includes the int property, SalutationID. salutation is just a list of lu_Salutation records. lu_Salutation is the foreign / lookup table that has columns, TitleID and Title. TitleID on this table maps to SalutationID on vEmployee. So, we would have something like: model.SalutationID == lu_Salutation.TitleID. My challenge is how to to call the @Html.Display in my partial view, supplying just the SalutationID, the LookupList, and salutation. Also, with this, what should LookupList contain? – user938455 Nov 12 '17 at 08:12
  • If you edit you question to add the models (including `lu_Salutation`) then an answer can be added :) –  Nov 12 '17 at 08:18
  • 1
    Also you have said `var salutations = (IEnumerable)ViewData["salutations‌​"];` is in the controller, which does not make sense - do you mean that code is in the main view? –  Nov 12 '17 at 08:20
  • //Entity Model Class (lookup table) 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; } } – user938455 Nov 12 '17 at 12:22
  • Yes, I know, but you need to edit your question to include the relevant details (and you still have not responded to my previous comment) –  Nov 12 '17 at 12:24
  • sorry, i could not edit the question, and i could not format the comment – user938455 Nov 12 '17 at 12:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158797/discussion-between-user938455-and-stephen-muecke). – user938455 Nov 12 '17 at 12:28
  • @user938455 Of course you can edit your question, use the [edit](https://stackoverflow.com/posts/47241899/edit) link. Please never post code as comment, it is unreadable. – Modus Tollens Nov 12 '17 at 12:32
  • I finally accepted chat suggestion from @StephenMuecke to simply add a text property to the viewmodel and capture the associated text from the controller. This way, there will be no need for a dropdown. Thanks all! – user938455 Nov 13 '17 at 22:08

0 Answers0