Introduction
There are the following three ways to pass a value or data from a view to an action:
- Parameterized Query.
- Form Data.
- Model Binding.
A QueryString is the data passed using the browser URL.
- What we will do in this application.
- We will learn how to retrieve the data using a QueryString parameter.
- For all this operation we will use the EntityFramework.
(The database script is available in the attached.)
Create this table inside your database.
- create table tbl_QueryStringUse
- (
- ID int identity primary key,
- Name varchar(50),
- Mobile bigint,
- EmailID varchar(100),
- Address varchar(100)
- )
Start Visual Studio 2010 then Start a new MVC 3 (with Razor View Engine).
Add a new Controller, the project is named “UseQueryString”.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MvcApplication3.Controllers
- {
- public class UseQueryStringController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- }
- }
Add a View to the Index() action method.
After adding the View:
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
- <h6>We will be learcn here how we can Retrieve data from a view by using Query String.</h6>
- <h6>@Html.ActionLink("Add New Rocord", "Insert", "UseQueryString")</h6>
Explanation
Run the project and test the output.
Add a new action method to the controller.
Add a new action method named “Insert()”.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MvcApplication3.Controllers
- {
- public class UseQueryStringController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult Insert()
- {
- return View();
- }
-
- }
- }
Add a new view to the action method.
After adding the View:
- @{
- ViewBag.Title = "Insert";
- }
-
- <h2>Insert</h2>
- <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>
- <h6>Insert New Records Here:</h6>
Design the form.
Note: To get a value with a Query String, we need to declare the form method as Get only.
Design the Form: “Insert.cshtml”
- @{
- ViewBag.Title = "Insert";
- }
-
- <h2>Insert</h2>
- <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>
- <h6>Insert New Records Here:</h6>
- @using (@Html.BeginForm("InsertNew", "UseQueryString", FormMethod.Get))
- {
- <table>
- <tr>
- <td>Enter Your Name</td><td>:</td><td>@Html.TextBox("TxtName")</td>
- </tr>
- <tr>
- <td>Enter Your Mobile No</td><td>:</td><td>@Html.TextBox("TxtMobile")</td>
- </tr>
- <tr>
- <td>Enter Your EmailID</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>
- </tr>
- <tr>
- <td>Enter Your Address</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>
- </tr>
- <tr>
- <td>Save Your Details</td><td>:</td><td><input type="submit" value="Submit" /></td>
- </tr>
- </table>
- }
Run the project and watch the output.
If you now click on the Submit button it will show you the error.
Now create the table if it is not created.
- create table tbl_QueryStringUse
- (
- ID int identity primary key,
- Name varchar(50),
- Mobile bigint,
- EmailID varchar(100),
- Address varchar(100)
- )
Add this table to the Entity Framework using the following procedure.
Double-click in the Model.edmx file.
Add this table to the Model designer.
Update the Model from the database.
Update Wizard
Select the table name here.
Watch it.
After adding, build the project.
At the Controller Action Method create the Table Object.
Write the code for inserting the data into the Controller.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MvcApplication3.Controllers
- {
- public class UseQueryStringController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult Insert()
- {
- return View();
- }
-
- public ActionResult InsertNew()
- {
-
-
-
- string _Name, _EmailID, _Address;
- long _MpbileNo;
-
-
- _Name = Request.QueryString["TxtName"];
-
- _MpbileNo = Convert.ToInt64(Request.QueryString["TxtMobile"]);
-
- _EmailID = Request.QueryString["TxtEmailID"];
- _Address = Request.QueryString["TxtAddress"];
-
-
-
- using (MVC_PracticeEntities Entity = new MVC_PracticeEntities())
- {
-
- tbl_QueryStringUse TblObj = new tbl_QueryStringUse();
-
- TblObj.Name = _Name;
- TblObj.Mobile = _MpbileNo;
- TblObj.EmailID = _EmailID;
- TblObj.Address = _Address;
-
-
- Entity.AddTotbl_QueryStringUse(TblObj);
-
-
- int Row = Entity.SaveChanges();
- if (Row > 0)
- {
- ViewBag.Msg = " Data Of "+ _Name + " Has been Saved Successfully. ";
- }
- }
- return View();
-
- }
-
- }
- }
Let's run the project and test it.
Add some data and save it.
Output
Add some more data and save it.
Go the SQL Server Management Studio and verify the results.
Just a minute.
I am providing all the code again.
Index.cshtml
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
- <h6>We will be learcn here how we can Retrieve data from a view by using Query String.</h6>
- <h6>@Html.ActionLink("Add New Rocord", "Insert", "UseQueryString")</h6>
Insert.cshtml
- @{
- ViewBag.Title = "Insert";
- }
-
- <h2>Insert</h2>
- <h6>@Html.ActionLink("Go Back", "Index", "UseQueryString")</h6>
- <h6>Insert New Records Here:</h6>
- @using (@Html.BeginForm("InsertNew", "UseQueryString", FormMethod.Get))
- {
- <table>
- <tr>
- <td>Enter Your Name</td><td>:</td><td>@Html.TextBox("TxtName")</td>
- </tr>
- <tr>
- <td>Enter Your Mobile No</td><td>:</td><td>@Html.TextBox("TxtMobile")</td>
- </tr>
- <tr>
- <td>Enter Your EmailID</td><td>:</td><td>@Html.TextBox("TxtEmailID")</td>
- </tr>
- <tr>
- <td>Enter Your Address</td><td>:</td><td>@Html.TextBox("TxtAddress")</td>
- </tr>
- <tr>
- <td>Save Your Details</td><td>:</td><td><input type="submit" value="Submit" /></td>
- </tr>
- </table>
- }
InsertNew.cshtml
- @{
- ViewBag.Title = "InsertNew";
- }
-
- <h2>InsertNew</h2>
- <h6>@Html.ActionLink("Go Back", "Insert", "UseQueryString")</h6>
- <h5>New Record Addedd Successfully.</h5>
-
- <h6>@ViewBag.Msg</h6>
UseQueryStringController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MvcApplication3.Controllers
- {
- public class UseQueryStringController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult Insert()
- {
- return View();
- }
-
- public ActionResult InsertNew()
- {
-
-
-
- string _Name, _EmailID, _Address;
- long _MpbileNo;
-
-
- _Name = Request.QueryString["TxtName"];
-
- _MpbileNo = Convert.ToInt64(Request.QueryString["TxtMobile"]);
-
- _EmailID = Request.QueryString["TxtEmailID"];
- _Address = Request.QueryString["TxtAddress"];
-
-
-
- using (MVC_PracticeEntities Entity = new MVC_PracticeEntities())
- {
-
- tbl_QueryStringUse TblObj = new tbl_QueryStringUse();
-
- TblObj.Name = _Name;
- TblObj.Mobile = _MpbileNo;
- TblObj.EmailID = _EmailID;
- TblObj.Address = _Address;
-
-
- Entity.AddTotbl_QueryStringUse(TblObj);
-
-
- int Row = Entity.SaveChanges();
- if (Row > 0)
- {
- ViewBag.Msg = " Data Of "+ _Name + " Has been Saved Successfully. ";
- }
- }
- return View();
-
- }
-
- }
- }