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

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

问题描述

我为了学习ASP.NET MVC工作基本的问题管理系统。我已经得到了它,并运行一个相当不错的水平,但我碰到的一个问题。

我有一个名为发行了一个名为开放式视图控制器。 /问题/打开列出了所有未决问题当前登录系统。我已经定义了一个路线,像这样:

  routes.MapRoute(
        OpenSort,//路线名称
        问题/打开/ {}排序,// URL带参数
        新{控制器=问题,行动=打开,排序=TimeLogged} //参数默认
    );

这是工作的罚款,到目前为止,使用IssueController.cs以下code:

 公众的ActionResult打开(字符串排序)
{
    VAR问题从i =在db.Issues那里i.Status ==打开排序依据i.TimeLogged升选择我;    开关(排序)
    {
        案ID:
            在db.Issues那里i.Status ==打开排序依据i.ID升从i =问题我选择;
            打破;        案TimeLogged:
            转到默认;        案技术员:
            在db.Issues那里i.Status ==打开排序依据i.Technician升序选择我的问题=从我;
            打破;        案客户:
            在db.Issues问题=从哪里i.Status ==打开排序依据i.Customer升序选择我;
            打破;        案类别:
            在db.Issues那里i.Status ==打开排序依据i.Category升序选择我的问题=从我;
            打破;        案优先级:
            在db.Issues问题=从哪里i.Status ==打开排序依据i.Priority升序选择我;
            打破;        案状态:
            在db.Issues那里i.Status ==打开排序依据i.Status升序选择我的问题=从我;
            打破;        默认:
            打破;
    }    计算机[标题] =打开的问题;
    计算机[SortID] = sort.ToString();    返回查看(Issues.ToList());
}

这是工作正常(虽然,我不知道是否有更好的方式来处理我的查询的定义不是交换机?),但现在我希望能够做的开放问题两件事情查看:


  1. 排序任何标题 - 确定

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

我无法弄清楚如何将两个参数传递给控制器​​,所以我可以安排我的查询。我也只是意识到除非我想出如何生成我的查询上飞,我会需要(排序选项数量)*在我的开关(过滤选项数)。

哎呀,任何人都可以点我在正确的方向?干杯!


解决方案

  1. 删除排序的路线。只需使用一个路由没有参数。

  2. 查询字符串参数添加到查询的排序,筛选等,所以您查询将看起来像:

<一个href=\"http://example.com/Issue/Open?sort=ID&filter=foo\">http://example.com/Issue/Open?sort=ID&filter=foo

 公众开放的ActionResult(串排序,字符串过滤器)

MVC框架将填补从查询字符串参数的参数。确保并使用可空类型(如字符串)任何这些查询字符串参数参数可能不被填充。

其实,我觉得这是一个比较正确的方式来写的URL。 URL本身标识资源(开放问题);查询字符串参数自定义如何显示资源。

至于查询数去,记住,你不必一次构建整个查询。您可以使用.OrderBy扩展方法重新整理现有的IQueryable&LT; T&gt;和同样用。凡

  VAR的问题,从我在那里db.Issues == i.Status打开选择I =;开关(排序)
{
    案ID:
        问题= Issues.OrderBy(ⅰ= GT; i.ID);
        打破;    // [...]    默认:
        问题= Issues.OrderBy(ⅰ= GT; 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到SQL生成对即时查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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