0 Replies - 772 Views - Last Post: 28 July 2017 - 02:05 PM

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,318
  • Joined: 12-December 12

[CORE] reduce context for cascading combos

Posted 28 July 2017 - 02:05 PM

ASP.NET CORE + Entity Framework

Say I have a Staff table and a StaffContext. This includes the fields:
StaffID, FirstName, LastName, Office, Department, Grade, Salary

I want the first page to be a set of cascading comboboxes for Office, Department, Grade. That is, the user will select an Office, then a related (filtered) Department, then Grade, and pressing Go it will move to displaying Staff records for just that Office/Department/Grade combination.

So, for this first page, I only need the set of data containing Offices, Departments, Grades. It is only on the next page that I'll need the full dataset.

How should I define the ViewModel for the first page? Something like:

public class StaffFilter {
    public string Office { get; set; }
    public string Department { get; set; }
    public int? Grade { get; set; }
public class FilterViewModel {
    List<StaffFilter> Filters { get; set; }

Would you include SelectedOffice, etc., in this ViewModel? I think I could just pass the three values in the ViewBag, otherwise I'd need yet another ViewModel(?).

My more pressing question is, how can I easily convert _context.Staff into new FilterViewModel for passing to the View? In particular, I don't want to manually iterate the context-table building a list.

It occurs to me that I could do (in outline):
FilterViewModel = context > LINQ > select new { StaffFilter } > ToList

but am I missing a trick? Is there a simple way to reduce the context-table to just three columns?

I suppose I'm just looking for a little reassurance that my approach is reasonable (it helps just writing this down ;)) but I will welcome any advice or suggestions/corrections.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1