In this article I will explain with an example, how to call 
MySQL Stored Procedure with Parameters in ASP.Net MVC.
 
    The article explains with a simple example where a 
Stored Procedure will be passed with a parameter and it returns the matching records. In similar way one can pass multiple parameters.
 
    
     
     
    Download and Install the MySQL Connector
    You will need to download and install the 
MySQL Connector in order to connect to the 
MySQL database in ASP.Net MVC.
 
    
     
     
    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
    The following 
Stored Procedure accepts a parameter 
custId Integer parameter and is matched with the 
CustomerId field of the 
Customers Table of 
MySQL database.
 
    
    
        DELIMETER //
        CREATE PROCEDURE Customers_GetCustomer(IN custId INT)
        BEGIN
            SELECT Name
                  ,Country
            FROM Customers
            WHERE CustomerId = custId;
        END //
        DELIMITER ;
     
     
     
    Model
    The Model class consists of following properties.
    
        public class CustomerModel
        {
            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 System.Data;
        using System.Configuration;
        using MySql.Data.MySqlClient;
     
     
     
    Controllers
    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 TextBox value i.e. CustomerId as parameter.
    An object of CustomerModel class is created and the connection is read from the Web.Config file.
    
     
    An object of 
MySqlCommand class is created and 
CustomerId is added parameter and using 
ExecuteReader the records are fetched from the 
MySQL database.
 
    
     
    Finally, the fetched record is returned to the View.
    
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
         
            [HttpPost]
            public ActionResult Index(int customerId)
            {
                CustomerModel customer = new CustomerModel();
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                string spName = "Customers_GetCustomerById";
                using (MySqlConnection con = new MySqlConnection(constr))
                {
                    using (MySqlCommand cmd = new MySqlCommand(spName, con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@custId", customerId);
                        con.Open();
                        using (MySqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                customer = (new CustomerModel
                                {
                                    Name = sdr["Name"].ToString(),
                                    Country = sdr["Country"].ToString()
                                });
                            }
                        }
                        con.Close();
                    }
                }
                return View(customer);
            }
        }
     
     
     
    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 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 a TextBox created using Html.TextBox Helper method and a Submit Button.
     
    Submitting the Form
    When the Submit Button is clicked then, the CustomerModel class object is checked for NULL and if it is not NULL then the fetched records are displayed in HTML Table.
    
        @model MySQL_Call_SP_MVC.Models.CustomerModel
        @{
            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))
            {
                <span>Search:</span>
                @Html.TextBox("CustomerId")
                <input type="submit" value="Search" />
                <hr/>
                if (Model != null)
                {
                    <table cellpadding="0" cellspacing="0">
                        <tr>
                            <th>Name</th>
                            <th>Country</th>
                        </tr>
                        <tr>
                            <td>@Model.Name</td>
                            <td>@Model.Country</td>
                        </tr>
                    </table>
                }
            }
        </body>
        </html>
     
     
     
    Screenshot
    
     
     
    Demo
    
     
     
    Downloads