0

I am creating an ASP.Net application that displays records from a SQLite database and incorporating filter options that are built from the records that are coming in since I do not know what kind of data will be in the columns. I have the filter in place and will create the HTML form that will display the field value and how many records with that value there are. However I am not sure how to grab that data since as far as I am aware, the receiving method in the controller will expect a matching parameter for each variable (HTML form: value="bob", the receiving method in the controller takes a parameter such as string bob). My code for generating the HTML form is:

<div>
   <text>Employment:</text> <br />
      @foreach (var employment in Model.Employment)
      {
         <input type="checkbox" value="{@employment.Key}"/>@employment.Key <text>(@employment.Value)</text> <br />
      }
   <hr />
</div>

Since there could be, for instance, 15 different selection chosen, how would I grab all of the values that are generated? Additionally, there are about 5 more sections like this for filter options. Is it possible to transmit all selections as a dictionary like

<key,<key,value>> 

and then parse through the nested dictionaries? The dictionaries would then hold the main category of filters(main key) and then each accepted filter option(parent's value/key) within that category with the a ture(for the parent's value's value).

Edit: Adding some addition code as requested. This is the class that represents an employee for a company:

    public class MemberModel
    {

        public string ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string EmailAddress { get; set; }
        public string PhoneNumber { get; set; }
        public string Position { get; set; }
        public string Department { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string EmploymentStatus { get; set; }
        public string Shift { get; set; }
        public string Manager { get; set; }
        public string Photo { get; set; }
        public string FavoriteColor { get; set; }
    }

This might not be necessary for the implementation but made sense to me for building this app. This is a class that contains a list of all employees and dictionaries that I use for building the filter options:

   public class TeamMembersModel
    {
        public Dictionary<string, int> Employment { get; set; }
        public Dictionary<string, int> Position { get; set; }
        public Dictionary<string, int> Shift { get; set; }
        public Dictionary<string, int> Department { get; set; }
        public Dictionary<string, int> Manager { get; set; }
        public List<MemberModel> Employees { get; set; }
    }

This code is a condensed version of how I build the form for filtering:

<form asp-action="" asp-controller="TeamMembers" method="get">

   <!-- Department -->
   <div>
      <text>Department:</text> <br />
      @foreach (var departments in Model.Department)
      {
      <input type="checkbox" value="@departments.Key" />@departments.Key <text>(@departments.Value)</text> <br />
      }
      <hr />
   </div>

<!-- This is repeated for each dictionary in the TeamMembersModel class -->

   <button type="submit" value="Filter">Filter</button>
</form>
  • You basically want to bind a class or List to the view.... that way your controller can parse it when you submit. When you say you "do not know what kind of data will be in the columns" well... that's fine... as long as you know what the columns are. These would be properties in your class that you bind. Posting more of your code would be good. – pcalkins Oct 11 '19 at 22:20
  • @pcalkins Thanks for your response. I have added the class that stores information for an employee, the class that stores all of the employees to be displayed and different dictionaries for displaying filter options, as well as the HTML for building the form. – Jonathan Kealoha Manoa Oct 11 '19 at 22:34
  • I recommend creating the checkboxes in the controller: https://stackoverflow.com/questions/37778489/how-to-make-check-box-list-in-asp-net-mvc Doesn't seem like you need the dictionaries at all... bind/populate the List in the controller and the view will have it. Filter logic would be done on post... return List to view. – pcalkins Oct 11 '19 at 23:02
  • Sorry, but I found your description a bit unclear. Are asking how to parse the data from the HTML data? If so, the easiest way would be to use an HTML parsing library such as [HTML Monkey](https://www.nuget.org/packages/SoftCircuits.HtmlMonkey/). Such a library will translate those `` tags into nodes that can easily be traversed. – Jonathan Wood Feb 19 '20 at 04:24

0 Answers0