How to use Querystring Parameters in Asp.Net MVC to Retrieve or Send Data

  Total Views : 7704
  Comments : 0
 
  Keywords : How to pass querystring in asp.net mvc, Asp.net mvc querystring parameters example,Asp.net mvc retrieve data using query string parameters, Get query string values in asp.net mvc exmaple

Here we will learn query stirng parameters in asp.net mvc with example. Generally query string is one of client side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc.

 

In asp.net mvc routing has a support for query strings in RouteConfig.cs let’s have a look on. Our routing class file RouteConfig.cs  will contain id as optional Parameter.

 

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

name: "Default",

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

defaults: new

{

controller = "Home",

action = "Index",

id = UrlParameter.Optional

});

}

In asp.net mvc we can pass query string with Optional Parameter and also without Optional Parameter.

 

Query strings with Optional Parameter

 

http://localhost:2151/MyHome/index?Name=sai&&Age=50 

 

Query strings without Optional Parameters 

 

http://localhost:2151/MyHome/index/1?Name=sai&&Age=50 

Create New Asp.Net MVC Application

We will learn querystrings with simple example for that Open visual studio studio à Go to File à Select New à Select Project

 

create new asp.net mvc project from visual studio 2012

 

After that you will see new dialog will pop up for selecting your Template and Project type. From Templates select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application and here we are giving name as “Tutorial9_Querystring” then finally click on ok button

 

create new asp.net mvc project from visual studio 2012

 

 After naming it just click on OK now new dialog will pop up for selecting template in that Select Basic template, view enginer as Razor and click ok like as shown below

 

select basic template to create new application in asp.net mvc

 

After that visual studio will create new project for us based on our configuration that will be like as shown below

 

after create new asp.net mvc application for querystring demo

 

Now we will create a Controller with Name MyHomeController

Adding New Controller (MyHomeController) in Application

To add controller just right click on Controller folder then select Add from list and inside that select controller.

 

add new controller in asp.net mvc application

 

After selecting controller a new dialog will popup with name Add Controller in that give name as "MyHomeController". In template we are going to select Empty MVC controller like as shown below

 

select template to create new controller for query string in asp.net mvc

 

After Crcting controller here is Code snippet of it.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial9_Querystring.Controllers

{

public class MyHomeController : Controller

{

//

// GET: /MyHome/

public ActionResult Index()

{

return View();

}

}

}

Creating index ActionMethod [With Optional Parameter]

After adding controller now let's add parameters to index action method. This action method has optional parameter (ID) with 2 other parameter Name and Department.

 

public ActionResult Index(string ID, string Name, string Department)

{

return View();

}

Save Application and Run it

Now let's run application and access URL. For calling this URL first we are going to call Controller [MyHome] then following with Action Method [Index] lastly with Optional Parameter [10] then query string  will be like [? Name=saineshwar&&Department=SoftwareDevelopment  ].

 

E.g. URL will be like  http://localhost:2151/MyHome/Index/10?Name=saineshwar&&Department=SoftwareDevelopment 

 

 Below is Completed snapshot of how to pass Query string

Query string format in asp.net mvc application

 

Output after passing Query string [With Optional Parameter].

 

Output of Query string parameters in asp.net mvc with example

 

Following is the snapshot while debugging

 

Query string example in debug mode in asp.net mvc application

Creating Details ActionMethod [Without Optional Parameter]

Now let’s create Action Method with Name Details and this method will be without optional parameters we are going to pass 2 parameter Name and Department that will be like as shown below 

 

public ActionResult Details(string Name, string Department)

{

string result = " Name= " Name " Department= " Department;

return Content(result);

}

Save Application and Running it

Now lets Run Application and access URL. For calling this URL first we are going to call Controller [MyHome] then following with ActionMethod [Details] then lastly query string that will be like as [? Name=saineshwar&&Department=SoftwareDevelopment]

 

E.g. URL will be like http://localhost:2151/MyHome/Details?Name=saineshwar&Department=SoftwareDevelopment 

 

Output of Query string parameters in asp.net mvc with example

 

Following is the snapshot while debugging

 

Query string example in debug mode in asp.net mvc application

Now we completed query string in asp.net mvc with accessing from URL now let’s call Action Method from Action Link from passing Query string. Calling Action method from Action Link with Passing Query string. 

 

With Optional Parameters

 

@Html.ActionLink("Call Index Action", "Index", new { ID = 10, Name = 3, Department = "test" })

Without Optional Parameters

 

@Html.ActionLink("Call Details Action", "Details", new { Name = 3, Department = "test" })

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User