views:

60

answers:

1

I've created a routing structure whereas the action part of the URL serves as a dynamic handler for picking a specific user created system name. i.e.

http://mysite.com/Systems/%5BSystemName%5D/Configure, where [SystemName] designates the name of the system they would like to configure.

The method that routes the system is the following:

public ActionResult Index(string systemName, string systemAction)
    {

        ViewData["system"] = _repository.GetSystem(systemName);
        if (systemAction != "")
        {
            return View(systemAction);
        }
        else
        {
            // No Id specified.  Go to system selection.
            return View("System");
        }
    }

The above method sets the system to configure and routes to a static method where the view is displayed and a form awaits values.

The question I have is that when I create my configuration view, I lose my posted values when the form is submitted because it routes back to the above Index controller. How can I determine if data is being posted when hitting my above Index controller so that I can make a decision?

Thanks! George

+1  A: 

Annotate the controller method that handles the POST like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string systemName, string systemAction)
{
   // Handle posted values.
}

You can have a different method in your controller that handles the GETs:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(string systemName, string systemAction)
{
   // No posted values here.
}

Note that, although I have copied the same method and parameters in each case, the signature for the second method (parameters and types) will have to be different, so that the two methods are not ambiguous.

The NerdDinner tutorial has examples of this.

Robert Harvey
Keep in mind of course that those two action names are ambiguous even with the HttpPostAttribute/HttpGetAttribute.
Nathan Taylor
True, you would have to change the signature on one of them.
Robert Harvey
I'm still not sure this solves my issue. I have one controller that routes everything through a central point and makes a dynamic choice on an action. I simply need to keep the same Index controller signature, but need the ability to route that system to the appropriate action and then determine if a post has occurred. Does this make sense?
George
You can check the RequestType to determine if you're POSTing.
Robert Harvey