What is Query String and How to use it

What does Query String mean?

A query string is the portion of a URL where data is passed to a Web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data). There are a number of ways to do this:

  • On most Web servers, you can use something like session state server-side. 
  • On the client, you can store via cookies. 
  • Or in the URL, you can store data via a query string.

------------------------------

How to use Query String in Visual Studio ASP.NET MVC 


We can use Query String using three Ways
  1. By URL
  2. By Hyper link
  3. By form

  1. By URL :

Go to HomeController.cs and add 'string name' as a parameter in “index” action method. (here we use “index” page to show data)

public ActionResult Index (string name)
{
      return view();
}



At this point, data reached to ActionMethod index of Home controller but we still don't send data from this action method to Index view
Now to send data from action method to view,there are two ways
  1. ViewBag
  2. ViewData
The difference between the syntax of these is as follows
ViewBag.f_name = waqas;
ViewData["f_name"] = waqas;

Now again go to HomeController.cs and add ViewBag line in the same Action Method

public ActionResult Index(string name)
{
      ViewBag.fName = name;
      return View();
}

Now we are sending data to index view ..Now we'll receave data at index view.
For this add following code in Index view 

<h1> You Enter @ViewBag.fname </h1> //we use @ while writing c# code. This is Razor Syntex

Now Run the project and Go to following URL

              localhost:2342/home/index? name='Irfan'

Now “You Enter Irfan” is Showing on Webpage. This is the method how we use Query String using URL
---------------------

      2. By Hyper Link

after adding the code to "Index Action method" and "Index View". Add the following hyper link link to the "Index" view

<a href="/home/success?name=Irfan"> Click Here </a>

Now Run the project, after clicking on this Link,
"You Enter Irfan" will will be shown on the webpage.

This is how we use Query String using Hyper link

---------------------

      3. By Form


Now we'll see how to use Query String using From.
Add following form in this “Index” view.

<form action="/Home/Success" method="post">
      <input type="text" id="name" name="name" />
      <input type="password" id="pass" name="pass" />
      <input type="submit" value="sign up" />
</form>

Now we'll create another Action Method and a View where we want to display data that we enter using form.
Create a ActionMethod in HomeController and named it “Success” (you can use any name)

public ActionResult Success( )
{
      return View();
}

Add a View in Home folder and named it Success.cshtml. (use the same name as Action method)
Now add the following code to the “Success” Action Method


public ActionResult Success(string name,string pass)
{
      ViewBag.fName = name;
      ViewBag.password = pass;
      return View();
}

we add Parameters to get data to this action method. We use ViewBag to send data to Success.cshtml View.
Now to receive data in Success.cshtml, we again use @ViewBag in “Success” View

<h1@ViewBag.fName </h1>
<h1@ViewBag.password </h1>

Now run the project and visit Index page. Add data in text fields and click on Submit.
That data will be displayed on Webpage.