views:

30

answers:

1

Hi, i was wondering if it was possible to filter which action is invoked based on a paramater in the query string.

For example, i have a grid with a radio button column to select an item in the grid. The grid is wrapped in a form and at the top of the grid are buttons to edit/delete the selected item. Clicking on the edit/delete buttons posts back and does abit of jquery magic to set a command property so i can distinguish between an edit and a post. I can then handle this by adding the HttpPost filter attribute to my action.

Now i need to add a search to the form. The easiest way for me to do this is to place the search form outside the existing form and set the method to get. This works but i have a case where the search form needs to be situated within my grid form. I understand i can't have nested forms therefore i have removed the form tags for the inner form but now when doing a search it will trigger a post request. If you are still following along you will see that this triggers the edit/delete action method but i really want to trigger the initial action but have an extra search paramater.

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'd like to be able 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");
}

Notice how i filter which action is invoked based on the command. Maybe i've got myself in a complete muddle here but MVC is quite new to me and i'd really appreciate it if someone could help.

Thanks

A: 

You could probably do it with a route constraint. E.g., I could do something like this:

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
}

And then program my routes as such:

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

Obviously you'd need to change your Index(...) actions so the parameter names are both "command", but that should at least help you in the right direction?

Matthew Abbott
Cheers, this should definitely lead me in the right direction. Thanks again.
nfplee