ASP.NET MVC ActionLink的保持和QUOT;老"路线参数 [英] ASP.NET MVC ActionLink keep "old" route arguments

查看:132
本文介绍了ASP.NET MVC ActionLink的保持和QUOT;老"路线参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两个环节进行过滤:

My two links to filter:

@Html.ActionLink("Customer 1", "Index", new { customer = 1  })
@Html.ActionLink("Project A", "Index", new { project = "A"  })

我的控制器,过滤:

My Controller with filtering:

public ViewResult Index(int? customer, int? project) {
        var query = ...

        if (customer != null) {
            query = query.Where(o => o.CustomerID == customer);
        }

        if (project != null) {
            query = query.Where(o => o.ProjectID == project);
        }
        return View(query.ToList());
}

我现在可以过滤在任的同时客户或项目,但不是两个!

I can now filter on either customer or project but not on both at the same time!

如果我点击客户1, URL =对象?客户= 1

如果我点击项目A, URL =对象?项目= A

我希望能够首先单击客户1,然后项目A和获得 URL =目标客户= 1&放大器;项目= A

I would like to be able to first click Customer 1 and then Project A and get url = Object?customer=1&project=a

这是可能的,或者我应该做它以另一种方式?

Is this possible or should I do it in another way?

谢谢!

推荐答案

要做到这一点,正确的方法是用您的各种参数的模型返回到你的看法。

The correct way to do this is to return a model with your various parameters to your view.

型号

public class TestModel {
   public int? Customer { get; set; }
   public int? Project { get; set; }
   public List<YourType> QueryResults { get; set; }
}

查看

@model Your.Namespace.TestModel

...

@Html.ActionLink("Project A", "Index", new { customer = Model.Customer, project = Model.Project })

控制器

public ViewResult Index(TestModel model) {
    var query = ...

    if (model.Customer != null) {
        query = query.Where(o => o.CustomerID == model.Customer);
    }

    if (model.Project != null) {
        query = query.Where(o => o.ProjectID == model.Project);
    }

    model.QueryResults = query.ToList();

    return View(model);
}

这篇关于ASP.NET MVC ActionLink的保持和QUOT;老&QUOT;路线参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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