Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:02:04+00:00 2026-05-16T00:02:04+00:00

i was wondering if it was possible to filter which action is invoked based

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:02:04+00:00Added an answer on May 16, 2026 at 12:02 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am wondering is that possible (best practice) to run two long running query
I'm wondering is it possible to hot develop spring application. For example. In Play
I am just wondering if it is possible to have a condition that passes
I'm wondering if it's possible to filter an NSArray with a NSPredicate using an
I was wondering how I would create an option to filter content based off
I was wondering if it is possible to have a modalpopup show up on
Wondering if it's possible to create a dynamic linq query using linq to objects.
I was wondering ( if possible ) if there was a program/tool/utility that when
Just toying with the SDK and I was wondering if possible a UITouch event
I was wondering if its possible to use the Google maps IFRAME code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.