【问题标题】:ASP.NET MVC - Filter which action to invoke based on the query stringASP.NET MVC - 根据查询字符串筛选要调用的操作
【发布时间】:2010-07-28 15:17:12
【问题描述】:

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

例如,我有一个带有单选按钮列的网格,用于选择网格中的一个项目。网格被包裹在一个表单中,并且在网格的顶部是用于编辑/删除所选项目的按钮。单击编辑/删除按钮回发并执行 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 对我来说很新,如果有人能提供帮助,我会非常感激。

谢谢

【问题讨论】:

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


    【解决方案1】:

    你可以通过路由约束来做到这一点。例如,我可以这样做:

    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(...) 操作,以便参数名称都是“命令”,但这至少应该有助于您朝着正确的方向前进?

    【讨论】:

    • 干杯,这肯定会引导我走向正确的方向。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 2012-03-29
    • 2019-07-29
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多