c# - Passing multiple parameters to controller in ASP.NET MVC; also, generating on-the-fly queries in LINQ-to-SQL -


I am working on a basic point management system for learning ASP.NET MVC. I made it fabulous and running on a very decent level but I have participated in a problem.

I have a controller whose name is open / currently open / list all open issues open on the system / open I have defined a route like this:

  routes New {controller = "problem", action = "open", sort = "timel ged"} / / parameter defaults with MapRoute ("OpenSort", // root name "Point / Open / {}}", // URL parameter );   

This is working fine by now, using the following code in IssueController.cs:

  Public ActionService Open (string sort) {var Issues = I in db. Where i.TimeLogged ascending selection from i.Status == "Open" order i. Switch (sort) {case "ID": Issues = i db.Issues I.Status == "Open" order i.id ascending selection i; break; Case "timed": Goto default; Case "technician": Issues = In DB Issues where i.Status == "Open" order i.Technician ascending selection i; break; Case "Client": Issues i = Customized i.Customer by i.Status == "Open" order in issues i = i db.Issues i; break; Case "Category": Issues = i to db.Issues where i.Status == "Open" order by i.Category Ascending Selection I; break; Case "priority": Issues = i to db.Issues where i.Status == "Open" order by i. Priority ascending selection i; break; Case "Status": Issues = i to db.Issues where i.Status == "Open" command i.Status ascending selection i; break; Default: Break; } View Data ["Title"] = "Open Issues"; ViewData ["SortID"] = Sort Toasting (); See Return (Issues. Technologist ()); }  

This is working fine (though, I wonder what better way to handle my definition of query than switch?) But now I have to do two things

  • Filter on some titles (technicians, customers, categories, preferences, Event) - ??
  • I can not understand how the controller has to pass two parameters so that I can organize my questions. I have also realized that till I do not understand how my questions are made on my fly, I will need to switch (the number of sort options) * (number of filter options) in my switch.

    Can anyone show me the right direction? Cheers!

    1. Remove sort by route Just use a path without any parameters.
    2. Add query string parameters for sort, filters, etc. Your query will look like this:

      open public actions (string sort, string filter)  

    MVC framework will fill arguments with query string parameters. Use any type of string (such as string) for any of the query string parameter arguments that are not filled in.

    I really think this is a "more correct" way of writing the URL. URL only recognizes resources (open issues); Query string parameters are customized to display resources.

    As far as the number of questions goes up, remember that you do not need to create the entire query at one time. You can create an existing IQueryable & lt; T & gt; And thus, you can use the Order By Extension method to reorder. where.

      var issues = i to db.Issues where i.Status == select "Open" i; Switch (sort) {case "id": issue = issue Orderbay (i => i.id); break; // [...] Default: Issues = Issues. Orderbie (i => i.TimeLogged); }  

    Comments