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

4 Comments

Comments have been disabled for this content.