Asp.net mvc ASP.NET MVC-根据查询字符串筛选要调用的操作

Asp.net mvc ASP.NET MVC-根据查询字符串筛选要调用的操作,asp.net-mvc,asp.net-mvc-2,actionfilterattribute,Asp.net Mvc,Asp.net Mvc 2,Actionfilterattribute,我想知道是否可以根据查询字符串中的参数筛选调用哪个操作 例如,我有一个带有单选按钮列的网格,用于选择网格中的项目。网格被包装在一个表单中,网格顶部是用于编辑/删除所选项目的按钮。单击编辑/删除按钮返回帖子,并使用jquery魔术设置命令属性,以便区分编辑和帖子。然后,我可以通过将HttpPost过滤器属性添加到我的操作来处理这个问题 现在我需要在表单中添加一个搜索。对我来说,最简单的方法是将搜索表单放置在现有表单之外,并将方法设置为get。这是可行的,但我有一个例子,搜索表单需要位于我的网格表单

我想知道是否可以根据查询字符串中的参数筛选调用哪个操作

例如,我有一个带有单选按钮列的网格,用于选择网格中的项目。网格被包装在一个表单中,网格顶部是用于编辑/删除所选项目的按钮。单击编辑/删除按钮返回帖子,并使用jquery魔术设置命令属性,以便区分编辑和帖子。然后,我可以通过将HttpPost过滤器属性添加到我的操作来处理这个问题

现在我需要在表单中添加一个搜索。对我来说,最简单的方法是将搜索表单放置在现有表单之外,并将方法设置为get。这是可行的,但我有一个例子,搜索表单需要位于我的网格表单中。我知道我不能嵌套表单,因此我删除了内部表单的表单标记,但现在在执行搜索时,它将触发post请求。如果您仍在继续,您将看到这会触发编辑/删除操作方法,但我确实希望触发初始操作,但有一个额外的搜索参数

以下是我的行动方法:

public ActionResult Index(string search)
{
    return GetData(search);
}

[HttpPost]
public ActionResult Index(string command, int id)
{
    switch (command)
    {
        case "Delete":
            DeleteData(id);
            break;
        case "Edit":
            return RedirectToAction("Edit", new { id = id });
    }

    return RedirectToAction("Index");
}
理想情况下,我想说:

public ActionResult Index(string search)
{
    return GetData(search);
}

[HttpPost]
[Command(Name="Delete,Edit")] OR [Command(NameIsNot="Search")]
public ActionResult Index(string command, int id)
{
    switch (command)
    {
        case "Delete":
            DeleteData(id);
            break;
        case "Edit":
            return RedirectToAction("Edit", new { id = id });
    }

    return RedirectToAction("Index");
}
请注意,我如何根据命令筛选调用的操作。也许我在这里弄得一团糟,但MVC对我来说是个新鲜事物,如果有人能帮我,我会非常感激


谢谢

您可能可以通过路由约束来完成。例如,我可以这样做:

public class CommandConstraint : IRouteConstraint
{
    #region Fields
    private string[] Matches;
    #endregion

    #region Constructor
    /// <summary>
    /// Initialises a new instance of <see cref="CommandConstraint" />
    /// </summary>
    /// <param name="matches">The array of commands to match.</param>
    public CommandConstraint(params string[] matches)
    {
        Matches = matches;
    }
    #endregion

    #region Methods
    /// <summary>
    /// Determines if this constraint is matched.
    /// </summary>
    /// <param name="context">The current context.</param>
    /// <param name="route">The route to test.</param>
    /// <param name="name">The name of the parameter.</param>
    /// <param name="values">The route values.</param>
    /// <param name="direction">The route direction.</param>
    /// <returns>True if this constraint is matched, otherwise false.</returns>
    public bool Match(HttpContextBase context, Route route, 
        string name, RouteValueDictionary values, RouteDirection direction)
    {
        if (Matches == null)
            return false;

        string value = values[name].ToString();
        foreach (string match in Matches)
        {
            if (string.Equals(match, value, StringComparison.InvariantCultureIgnoreCase))
                return true;
        }

        return false;
    }
    #endregion
}

显然,您需要更改索引(…)操作,以便参数名称都是“command”,但这至少可以帮助您朝着正确的方向前进?

干杯,这肯定会引导我朝着正确的方向前进。再次感谢。
routes.MapRoute("Search", "Home/{command}",
                new
                {
                    controller = "Home",
                    action = "Index",
                    command = UrlParameter.Optional
                },
                new { command = new CommandConstraint("Search") });

routes.MapRoute("Others", "Home/{command}/{id}",
                new
                {
                    controller = "Home",
                    action = "Index",
                    command = UrlParameter.Optional,
                    id = UrlParameter.Optional
                },
                new { command = new CommandConstraint("Delete", "Edit") });