gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:01:27 26 4
gpt4 key购买 nike

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

例如,我有一个带有单选按钮列的网格,用于选择网格中的项目。网格包裹在一个表单中,网格顶部是用于编辑/删除所选项目的按钮。单击编辑/删除按钮回发并执行一些 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com