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

后端 未结 5 859
南方客
南方客 2020-12-08 01:05

I\'m working on a basic Issue Management System in order to learn ASP.NET MVC. I\'ve gotten it up and running to a fairly decent level but I\'ve run into a problem.

5条回答
  •  天涯浪人
    2020-12-08 02:00

    1. Remove sort from the route. Just use a route without a parameter.
    2. Add query string parameters to the query for the sort, filter, etc. So your query will look like:

    http://example.com/Issue/Open?sort=ID&filter=foo

    public ActionResult Open(string sort, string filter)
    

    The MVC framework will fill in the arguments from the query string parameters. Make sure and use nullable types (like string) for any of these query string parameter arguments which might not be filled in.

    I actually think this is a "more correct" way to write the URL. The URL itself identifies the resource (open issues); the query string parameters customize how to display the resource.

    As far as the number of queries go, remember that you do not have to build the entire query at once. You can use the .OrderBy extension method to re-order an existing IQueryable, and similarly with .Where.

    var Issues = from i in db.Issues where i.Status == "Open" select i;
    
    switch (sort)
    {
        case "ID":
            Issues = Issues.OrderBy(i => i.ID);
            break;
    
        // [...]
    
        default:
            Issues = Issues.OrderBy(i => i.TimeLogged);
    }     
    

提交回复
热议问题