In this article I will explain with an example, how to pass parameter to
Stored Procedure using
Dapper library in ASP.Net MVC.
Installing Dapper package using Nuget
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts
Country as a parameter, which is used to display the records from
Customers Table.
CREATE PROCEDURE [Customers_GetCustomersByCountry]
@Country VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT [CustomerId]
,[Name]
,[Country]
FROM [Customers]
WHERE [Country] = @Country
END
Model
The Model class consists of following properties.
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Namespaces
You will need to import the following namespaces.
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Controller
The Controller consists of following Action methods.
Action Method for handling GET operation
Inside this Action method, simply the View is returned.
Action Method for handling POST operation
Inside this Action method, the Country value is passed as a parameter and records are fetched from the
Customers Table of the
SQL Server database using
Stored Procedure.
Then, DynamicParameter class object is created and the Country value is added as parameter.
Finally, the name of the
Stored Procedure and an object of
DynamicParameter class with
CommandType as
Stored Procedure are passed to parameter and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string country)
{
string spName = "Customers_GetCustomersByCountry";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
DynamicParameters dynamicParameters = new DynamicParameters();
// Adding parameter.
dynamicParameters.Add("@Country", country);
return View(con.Query<Customer>(spName, dynamicParameters, commandType: CommandType.StoredProcedure));
}
}
}
View
HTML Markup
Inside the View, in the very first line the Customer Model is declared as Generic List collection.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The View also consists of an HTML Table, which consists of DropDownList created using HTML.DropDownList method respectively and a Submit Button.
Then, the Generic List collection of Customer class is checked for NULL and if it is not NULL then a FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@using Dapper_Parameter_SP_MVC.Models
@model List<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<td>Country: </td>
<td>
@Html.DropDownList("Country", new List<SelectListItem>
{
new SelectListItem { Text = "United States", Value = "United States" },
new SelectListItem { Text = "India", Value = "India" },
new SelectListItem { Text = "France", Value = "France" },
new SelectListItem { Text = "Russia", Value = "Russia" }}, "Please select")
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
@if (Model != null)
{
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>Customer Id</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerId</td>
<td>@customer.Name</td>
<td>@customer.Country</td>
</tr>
}
</table>
}
</body>
</html>
Screenshot
Downloads