views:

212

answers:

1

Hello,

I have an ASP.NET MVC application that has one part where I dont really want to use the auto url feature. I have a significant number of OPTIONAL parameters that need to pass in. This is basically for a complex query form... and a parameter that is not in use (i.e. has the default value) should please not be part of the URL. I love the URL routing for all other elements, but it is really not appropriate here. It does not reall make things more readable to have 20 or so fields in the URL, with 17 being empty.

The main problem I have is the generaton of the action link. On the partial view that is the query editor I want to have an action link generated that points to the results page. They both share the same model (and all parameters are properties).

Is there a method that can generate me the query string parameters to use starting from the model? The othe rway (binding fields to model properties) is already in by default, but I need a way to generate the query string.... preferably automatically.

+1  A: 

What you are describing sounds like a simple if tree:

StringBuilder myQueryString = new StringBuilder();

if (parameter1 != null)
    myQueryString.Append("&Parameter1=" + parameter1.ToString());

if (parameter2 != null)
    myQueryString.Append("&Parameter2=" + parameter2.ToString());

Assuming at least one parameter is already in the querystring, of course.

Robert Harvey
Not really right.... because this is server side.The main question probably goes around the client side - the generation of the action link. How do I do that best - after all, i need to check every possible field whether it is the default value or not, then generate the parameter string. QUITE a lot of work, would you not say? Is there no support for that in the ASP.NET MVC framework?
TomTom
Being a client-side problem, you can write Javascript or jQuery to solve it, or you could post your values back to a controller method, create the desired Url, and then forward the page to your generated Url, displaying the results of the search. The latter approach might be easier, especially if you're not keen on writing Javascript or jQuery.
Robert Harvey
Ok, so MVC 2.0 has no internal provision (a helper method) for doing the same and basically generating a client side call for this? That would close it - and mark this as answer, sadly ;)
TomTom
A helper method is not going to work, because it will already have executed before the user has a chance to fill in the search fields. I would simply use two controller methods. The first controller method will handle the post and generate the URL, and the second will take in the parameter values and display the result. Don't be sad; it's a good approach. :)
Robert Harvey
Well, I thought more of a helper method that emits the proper script on the serve rto generate the URL, saving me that redirect. Will go that approach, though - post back, then generate a redirect to the URL with all parameters.
TomTom