QueryString values along with id parameter in ASP.NET MVC

 A number of folks have asked me how to pass query string values along with a route parameter id. They are looking onto send a url like  http://MySite/Home/Edit/5?name=shiju.

The below is the routing configuration

 

routes.MapRoute(

    "Default",                                            

    "{controller}/{action}/{id}",                        

    new { controller = "Home", action = "Index", id = UrlParameter.Optional }

);

 

 

We need to pass route information for id parameter and also attach query string values. The below is our action method

 

public ActionResult Edit(int id,string name)

{

    //To Do

    return View();

}

 The below action method would set values for id parameter and query string parameter name.

<%=Html.ActionLink("Edit","Edit",new { id=5,name="shiju"}) %>

The ActionLink helper would generate the following markup

<a href="/Home/Edit/5?name=shiju">Edit</a>

 

Our Action method would automatically taken the value 5 for id and value "shiju" for name. This is really nice feature and convention of ASP.NET MVC
Published Friday, February 26, 2010 12:44 AM by shiju
Filed under: ,

Comments

# re: QueryString values along with id parameter in ASP.NET MVC

Friday, February 26, 2010 12:59 PM by thangchung

Thank to Shiju, it's very useful to me!

# ASP.NET MVC Archived Blog Posts, Page 1

Sunday, February 28, 2010 11:24 PM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# re: QueryString values along with id parameter in ASP.NET MVC

Wednesday, August 25, 2010 8:02 PM by Mumtaz Ali

Hi, I am using query in my mvc application like /product/edit/5, I think I should change it because users can change id parameter and access product, I have validation there for user to access product, what you suggest, should I continue with it or should I change it as application has many users

jcreator16@hotmail.com

# re: QueryString values along with id parameter in ASP.NET MVC

Thursday, January 13, 2011 5:05 PM by Jon Worth

How do you pass a bookmark in the url string...like /product/edit/5#tab=2

Seems like MVC overlooked support for bookmarks in the Url String.  I can't seem to find anything about it.

# re: QueryString values along with id parameter in ASP.NET MVC

Tuesday, March 08, 2011 5:36 AM by satish

how to pass values

# re: QueryString values along with id parameter in ASP.NET MVC

Thursday, June 02, 2011 11:04 PM by Mazlan

@Jon Worth. Me too, I still finding a way to deal with # hash URL in route.

# -=:: Post Topics ::=- &laquo; Stack 24/7

Friday, July 01, 2011 8:29 PM by -=:: Post Topics ::=- « Stack 24/7

Pingback from  -=:: Post Topics ::=- &laquo; Stack 24/7

# re: QueryString values along with id parameter in ASP.NET MVC

Wednesday, January 04, 2012 8:40 AM by Ashish Tripathi

Thanks Shiju your post is really helpful.

# re: QueryString values along with id parameter in ASP.NET MVC

Monday, March 12, 2012 3:38 AM by mrscript

Great Post

Leave a Comment

(required) 
(required) 
(optional)
(required)