3

I have a URL:

Shipment/Search/{searchType}/{searchValue}

and a controller action:

// ShipmentSearchType is an enum ... PartNumber, CustomerOrder, etc...
ActionResult Search(ShipmentSearchType searchType, string searchValue)

So this means I can type in pretty urls like:

Shipment/Search/PartNumber/Widget-01

And get a list of all the shipments with that part number.

Now I'm doing the busy work of the application and got to the point where I am making a search form that asks for Part Number and it will post back to Search. So basically I want:

Shipment/Search/PartNumber/{user-input-from-textbox}

Unfortunately I can't have a form get to the above url -- it has to be generated server side. So instead I'll have the form post back to Shipment/Search/PartNumber with {user-input} as a post request value.

So I end up with:

[AcceptVerbs(HttpVerbs.Post)]
ActionResult Search(ShipmentSearchType searchType, string searchValue, bool? post)
{
    return RedirectToAction("Search", new { searchType = searchType, searchValue = searchValue});
}

2 things:

1) Is there a way I can get around having the post method of Search without using javascript on the client side?

2) The bool? post value is there just so they have different signatures. That is obviously ugly. Is there a smarter way to do this?

Thanks!

edit:

"Unfortunately I don't think I can do that from a form (without javascript at least)." & "Is there a way I can get around having the post without using javascript?"

This was a bit ambiguous. What I mean is that I don't think I can have a form generate the url /Shipment/Search/PartNumber/{value-from-textbox} and have it to a form method get. I think this would be simple to do in javascript (override the submit action to build the url dynamically) but I haven't done that. I didn't mean that javascript is necessary to do a post.

1
  • I don't see a reason why you cannot do that without javascript. Could you explain why you think you can't? Commented Apr 27, 2009 at 15:48

1 Answer 1

6

i have same situation, but it work fine without javascript, i just receive FormCollections in [post]Search and then redirect to action like this:

[AcceptVerbs(HttpVerbs.Post)]
ActionResult Search(FormCollection form)
{    return RedirectToAction("Search", new { searchType = form["searchType"], searchValue = form["searchValue"]}); }

I think it is a good solution because i watched video about Post-Redirect-Get pattern which - good practicies in asp.net mvc applications.

Sign up to request clarification or add additional context in comments.

3 Comments

Pattern description you can find here en.wikipedia.org/wiki/Post/Redirect/Get video is videos.visitmix.com/MIX09/T44F it is not about pattern but Phil Haack said about it here.
I guess I don't see that as a solution. All you are doing is obfuscating the method signature to use a form collection but doing the exact same thing as: ActionResult Search(ShipmentSearchType searchType, string searchValue) This is just a different work around for not being able to have identical signatures for the Get and Post method. My question (which ended up being confusing!) was if there was a way to have a Get and Post with the same signature without artificially differentiating them. I can get it to work (and it is) I just find it smelly.
Agreed with eyston, but at least it works, so thanks for that.

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.