asp.net-mvc - ASP.NET MVC - 根据查询字符串过滤要调用的操作

标签 asp.net-mvc asp.net-mvc-2 actionfilterattribute

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

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

现在我需要在表单中添加搜索。对我来说,最简单的方法是将搜索表单放在现有表单之外,并将方法设置为获取。这行得通,但我有一个情况,搜索表单需要位于我的网格表单中。我知道我不能有嵌套表单因此我已经删除了内部表单的表单标签但是现在在进行搜索时它会触发发布请求。如果您仍在继续,您会看到这会触发编辑/删除操作方法,但我真的想触发初始操作但有一个额外的搜索参数。

这是我的操作方法的样子:

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
}

然后这样规划我的路线:

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

显然您需要更改您的 Index(...) 操作,以便参数名称都是“命令”,但这至少应该能帮助您朝着正确的方向前进?

关于asp.net-mvc - ASP.NET MVC - 根据查询字符串过滤要调用的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3354355/

相关文章:

asp.net-mvc - 在 MVC 应用程序中构建 Web 服务 API (SOAP)?

c# - 为什么我的 ASP.NET Web API ActionFilterAttribute OnActionExecuting 没有触发?

asp.net-mvc - 支持 RavenDB 的 MVC4 成员(member)系统

asp.net-mvc - 如何从编辑器模板添加 JavaScript 或 css 引用

asp.net - 我的 asp.net mvc 2.0 应用程序失败,错误为 "No parameterless constructor defined for this object"

asp.net - 如何使用和/或本地化 DisplayAttribute 与 ASP.NET MVC2?

asp.net-mvc - 不同 Action 的相同 Action 过滤器

asp.net-mvc-4 - 在 ActionFilterAttribute 验证时停止响应

c# - 位置 inheritInChildApplications 杀死调试器?

asp.net-mvc - Kendo Grid 自动更改时区