In this article I will explain with an example, how to pass parameter to
Stored Procedure using
Dapper library in ASP.Net Core MVC (.Net Core).
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;
Controller
Inside the Controller the IConfiguration class is injected into the Constructor (HomeController) with using Dependency Injection method.
Finally, the injected object is assigned to the Configuration property.
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
This Action method accepts Country value as parameter.
Inside this Action method, a DynamicParameters class object is created and Country value is passed as a parameter.
Finally, the name of the
Stored Procedure and the
DynamicParameters class object is passed to the
Query method of the
Dapper library and returned to the View.
public class HomeController : Controller
{
public IConfiguration Configuration { get; set; }
public HomeController(IConfiguration _configuration)
{
this.Configuration = _configuration;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string country)
{
string spName = "Customers_GetCustomersByCountry";
string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
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 CustomerModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – 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.
Submiting the Form
When the Submit button is clicked, the Customer is checked for NULL and if it is not NULL then, records are displayed using HTML Table. 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_Core_MVC.Models
@model List<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<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>
</form>
@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