ASP.NET MVC - filter, action for calling based on query string

I was wondering if it is possible to filter which action is called based on paramater in the query string.

For example, I have a grid with a switch column to select an item in the grid. The grid is wrapped in a shape, and at the top of the grid are buttons for editing / deleting the selected element. Pressing the edit / delete buttons returns messages and makes it impossible to use jquery magic to set the command property, so I can distinguish between editing and message. Then I can handle this by adding the HttpPost filter attribute to my action.

Now I need to add a search to the form. The easiest way to do this is to place the search form outside the existing form and set the receiving method. This works, but I have a case where the search form should be located in my grid form. I understand that I do not have nested forms, so I deleted the form tags for the internal form, but now when I perform a search, it calls for a submit request. If you still follow, you will see that this calls the edit / delete method, but I really want to initiate the initial action, but you have an additional search parameter.

Here's what my action methods look like:

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

Ideally, I would like to say:

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 , , - .

+3
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 (...), "", ?

0

Source: https://habr.com/ru/post/1756969/


All Articles