在 ASP.NET MVC 中将多个参数传递给控制器​​;此外,在 LINQ-to-SQL 中生成即时查询 [英] Passing multiple parameters to controller in ASP.NET MVC; also, generating on-the-fly queries in LINQ-to-SQL

查看:17
本文介绍了在 ASP.NET MVC 中将多个参数传递给控制器​​;此外,在 LINQ-to-SQL 中生成即时查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了学习 ASP.NET MVC,我正在开发一个基本的问题管理系统.我已经启动并运行到相当不错的水平,但我遇到了问题.

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.

我有一个名为 Issue 的控制器,有一个名为 Open 的视图./Issue/Open 列出当前记录在系统上的所有未解决的问题.我已经定义了这样的路线:

I have a controller named Issue with a view called Open. /Issue/Open lists all of the open issues currently logged on the system. I've defined a route like so:

    routes.MapRoute( 
        "OpenSort",                                                         // Route name
        "Issue/Open/{sort}",                                                // URL with parameters
        new { controller = "Issue", action = "Open", sort = "TimeLogged" }  // Parameter defaults
    );

到目前为止工作正常,在 IssueController.cs 中使用以下代码:

This is working fine so far, using the following code in IssueController.cs:

public ActionResult Open(string sort)
{            
    var Issues = from i in db.Issues where i.Status == "Open" orderby i.TimeLogged ascending select i;

    switch (sort)
    {
        case "ID":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.ID ascending select i;
            break;

        case "TimeLogged":
            goto default;

        case "Technician":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.Technician ascending select i;
            break;

        case "Customer":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.Customer ascending select i;
            break;

        case "Category":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.Category ascending select i;
            break;

        case "Priority":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.Priority ascending select i;
            break;

        case "Status":
            Issues = from i in db.Issues where i.Status == "Open" orderby i.Status ascending select i;
            break;

        default:
            break;
    }            

    ViewData["Title"] = "Open Issues";
    ViewData["SortID"] = sort.ToString();

    return View(Issues.ToList());
}

这工作正常(尽管我想知道是否有比 switch 更好的方法来处理我的查询定义?)但现在我希望能够在 Open Issues 视图上做两件事:

This is working fine (although, I wonder if there is a better way to handle my definition of the query than a switch?) but now I want to be able to do two things on the Open Issues view:

  1. 按任何标题排序 - 确定
  2. 过滤某些标题(技术员、客户、类别、优先级、状态)- ??

我不知道如何将两个参数传递给控制器​​,以便我可以组织我的查询.我也刚刚意识到,除非我弄清楚如何即时生成我的查询,否则我将需要(排序选项的数量)*(过滤器选项的数量)在我的开关中.

I can't figure out how to pass two parameters to the Controller so I can organise my queries. I've also just realised that unless I figure out how to generate my queries on the fly I am going to need (number of sort options) * (number of filter options) in my switch.

啊,有人能指出我正确的方向吗?干杯!

Argh, can anyone point me in the right direction? Cheers!

推荐答案

  1. 从路由中删除排序.只需使用不带参数的路由即可.
  2. 向查询添加查询字符串参数以进行排序、过滤等.因此您的查询将如下所示:

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

public ActionResult Open(string sort, string filter)

MVC 框架将从查询字符串参数中填充参数.确保对这些可能未填写的任何查询字符串参数参数使用可空类型(如字符串).

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.

我实际上认为这是编写 URL 的更正确"的方式.URL 本身标识资源(未解决的问题);查询字符串参数自定义如何显示资源.

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.

就查询的数量而言,请记住您不必一次构建整个查询.您可以使用 .OrderBy 扩展方法对现有的 IQueryable 重新排序,与 .Where 类似.

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<T>, 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);
}     

这篇关于在 ASP.NET MVC 中将多个参数传递给控制器​​;此外,在 LINQ-to-SQL 中生成即时查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆