1

I have a controller that is accepting 3 values from a submitted form, this works fine.

What I need to do now, however is enable a generated link to post the same data to the controller.

The controller looks like this:

[HttpPost]
public ActionResult Customer(string lastName, string postCode, string quoteRef)
{

 // Using passed parameters here

}

I am aware routing allows prettier URL's but in this case I need the form to be able to accept the three values either through the submitted form or by the following hyperlink format:

path.to.url/Home/Customer?lastName={1}&postcode={2}&quoteRef={3}

I have looked into routing but I can't find anything that will allow me to achieve this outcome.

My routes are currently set up as the following:

routes.MapRoute(
                "Customer",
                "{controller}/{action}/{id}/{lastName}/{postCode}/{quoteRef}", 
                new {controller = "Home", action = "Customer", id = ""}
                );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id =                           UrlParameter.Optional }
);

2 Answers 2

3

Just set the form action method to use GET instead of POST to send the parameters as query string

@using (Html.BeginForm("Customer", "Controller", FormMethod.Get

Decorate action method with HttpGet

[HttpGet]
public ActionResult Customer(string lastName, string postCode, string quoteRef)
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

just to add to that he will also need to lose [HttpPost] from his method declaration ;)
@fr0s1yjack, glad to hear :)
2

path.to.url/Home/Customer?lastName={1}&postcode={2}&quoteRef={3}

this would work only for [HttpGet] attribute, not [HttpPost]

So your method should look like:

[HttpGet]
public ActionResult Customer(string lastName, string postCode, string quoteRef)
{

// Using passed parameters here

}

And if you still want to use [HttpPost], you should make a little diffrence between GET and POST method, like here - you cannot have these methods with identical parameters.

URL: path.to.url/Home/Customer?lastName={1}&postcode={2}&quoteRef={3}&method={4}

[HttpGet]
public ActionResult Customer(string lastName, string postCode, string quoteRef, string method)
    { .. }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.