代码之家  ›  专栏  ›  技术社区  ›  nfplee

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

  •  3
  • nfplee  · 技术社区  · 14 年前

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

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

    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对我来说还是个新鲜事物,如果有人能帮上忙,我会非常感激的。

    谢谢

    1 回复  |  直到 14 年前
        1
  •  0
  •   Matthew Abbott    14 年前

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

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