Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Creating non-variable querystrings using Action Link helpers

, 27 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create non-variable querystrings using Action Link helpers.

Intro

This post is more kind of tip. The action link HTML helpers really simplifies our job in generating hyperlinks. These HTML helpers are integrated with the routing infrastructure and that helps to generate links very smartly. There are lot of overloaded versions available but all of them take the route values as anonymous objects.

@Html.ActionLink
(
    "Purchase Product",  // link text
    "Purchase",  // action name
    new { userName = Model.User  } // route values as an anonymous object
)

Sometimes the parameters that needs to be passed to the action don't need to be in routes but instead they have to be in query-strings.

An example, http://mapservices.com/location/show?lat=12.12&lon=23.5.

In these cases also the built-in action link extensions works just fine because still we can pass the lat and lon querystring variables wrapped in an anonymous object new { lat = 12.2, lon = 23.5 }.

Suppose we need to generate an URL like below:

http://mapservices.com/location/show?pos.lat=12.12&pos.lon=23.5

The querystring names contains a "." operator and when you use an anonymous object to pass these values as new { pos.lat = 12.12, pos.lon = 23.5 } you will run into an exception. How we can fix this problem still using the built-in mechanism is the rest of this post.

RouteValueDictionary

The route values we pass as anonymous object are convereted into RouteValueDictionary that will be later used to generate the href for the anchors. There are overloaded action link helpers available that directly accepts RouteValueDictionary as parameter. As a dictionary we can easily pass querystrings that contains "." as below,

@Html.ActionLink
(
    "Show the location", 
    "Show", 
    "Location", 
    new RouteValueDictionary<string, object> {{"pos.lat",12.12}, {"pos.lon",23.5}}, 
    null
)    

Updates

Removed the custom action link helper because we can directly pass the RouteValueDictionary to existing helpers - 7/27/2012. 

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

After2050
Software Developer Trigent Software Private Limited
India India
I'm a software developer from south tip of India. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and admire it is the most beautiful language ever seen.
 
I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.
 
I believe in small things and they makes me happy!
Follow on   Twitter

Comments and Discussions

 
GeneralBetter Option Pinmemberbruno123410-Jul-12 9:58 
GeneralRe: Better Option PinmemberAfter205010-Jul-12 19:22 
GeneralRe: Better Option Pinmemberbruno123412-Jul-12 9:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by After2050
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid