在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

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

问题描述

我正在开发一个基本的问题管理系统,以学习ASP.NET MVC。我已经得到了运行到一个相当体面的水平,但我遇到了一个问题。



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

  routes.MapRoute(
OpenSort,//路线名称
Issue / Open / {sort},//带参数的URL
new {controller =Issue,action =Open,sort =TimeLogged} //参数默认值
);

到目前为止,使用IssueController.cs中的以下代码,这是很好的工作:

  public ActionResult Open(string sort)
{
var Issues = from i in db.Issues where i.Status == 打开orderby i.TimeLogged升序select i;

switch(sort)
{
caseID:
Issues = from i in db.Issues where i.Status ==Openorderby i。 ID升序选择i;
break;

caseTimeLogged:
goto default;

case技术员:
Issues = from i in db.Issues where i.Status ==Openorderby i.Technician ascending select i;
break;

caseCustomer:
Issues = from i in db.Issues where i.Status ==Openorderby i.Customer ascending select i;
break;

caseCategory:
Issues = from i in db.Issues where i.Status ==Openorderby i.Category ascending select i;
break;

casePriority:
Issues = from i in db.Issues where i.Status ==Openorderby i.Priority ascending select i;
break;

caseStatus:
Issues = from i in db.Issues where i.Status ==Openorderby i.Status ascending select i;
break;

默认值:
break;
}

ViewData [Title] =开放问题;
ViewData [SortID] = sort.ToString();

return View(Issues.ToList());
}

这是工作正常(虽然,我想知道是否有更好的方法处理我的查询定义比一个切换?)但现在我想要能够在开放问题视图上做两件事情:


  1. 按任意标题排序 - OK

  2. 过滤某些标题(技术员,客户,类别,优先级,状态) - ??

我不知道如何传递两个参数到控制器,所以我可以组织我的查询。我也刚刚意识到,除非我想出如何生成我的查询,我需要(排序选项的数量)*(过滤器选项的数量)在我的开关。



Argh,任何人都能指向正确的方向?干杯!

解决方案


  1. 从路线中移除排序。

  2. 将查询字符串参数添加到排序,过滤器等的查询中。因此,您的查询将如下所示:

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

public ActionResult Open(string sort,字符串过滤器)

MVC框架将填充来自查询字符串参数的参数。请确保并为可能未填写的任何这些查询字符串参数参数使用可空类型(如字符串)。



我实际上认为这是一个更正确方式来写URL。 URL本身标识资源(开放问题);查询字符串参数自定义如何显示资源。



至于查询的数量,请记住,您不必一次构建整个查询。您可以使用.OrderBy扩展方法重新排序现有的IQueryable< T> ;,并与.Where类似。

  var问题= from i in db.Issues where i.Status ==Openselect i; 

switch(sort)
{
caseID:
Issues = Issues.OrderBy(i => i.ID);
break;

// [...]

默认值:
Issues = Issues.OrderBy(i => i.TimeLogged);
}


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.

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
    );

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());
}

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. Sort by any of the headings - OK
  2. Filter on certain headings (Technician, Customer, Category, Priority, Status) - ??

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. 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<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天全站免登陆