Handle Multiple Operations In a Single Action Method in MVC

Here is my Solution Explorer for my project.

  • I have added a controller called Homecontroller.
  • Added 2 classes to my Model Folder.
  • My connection class in my model contains the connection string with all the operation for CRUD operations. Here I am showing this class.
  • The Register class in the model contains all the database fields with the logic.

The following is my Connection.cs:

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using Forms.Models;  
  7.   
  8. namespace Forms.Models   
  9. {  
  10.     public class Connection  
  11.     {  
  12.         public SqlCommand cmdData = new SqlCommand();  
  13.         public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);  
  14.         public SqlDataAdapter dataAdapter = new SqlDataAdapter();  
  15.         DataTable dt = new DataTable();  
  16.   
  17.         private string connStr = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;  
  18.   
  19.         public Boolean funSPNonExecuteData(String SPName, SqlParameter[] sqlParams)   
  20.         {  
  21.             SqlCommand cmdData = new SqlCommand();  
  22.             cmdData.CommandText = SPName;  
  23.             cmdData.CommandType = CommandType.StoredProcedure;  
  24.             cmdData.Connection = funGetConnection();  
  25.             for (int i = 0; i < sqlParams.Length - 1; i++)  
  26.             {  
  27.                 cmdData.Parameters.AddWithValue(sqlParams[i].ParameterName, sqlParams[i].Value);  
  28.             }  
  29.             cmdData.ExecuteNonQuery();  
  30.             return true;  
  31.         }

  32. public SqlConnection funGetConnection()
    {
    if (conn.State == ConnectionState.Closed)
    {
    conn.Open();
    }
    return conn;
    }
  33.     }  
  34. }  
The following is the Registration Model Class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7. using MVCPROJECT.Models;  
  8. using Forms.Models;  
  9. namespace MVCPROJECT.Models  
  10. {  
  11.     public class Registration   
  12.     {  
  13.         Connection objConnection = new Connection();#region "Properties"  
  14.         SqlDataAdapter ds = new SqlDataAdapter();  
  15.         DataTable dt = new DataTable();  
  16.         public string Fname {get;set;}  
  17.         public string SName {get;set;}  
  18.         public string Lname {get;set;}  
  19.         public string Id {get;set;}  
  20.         public string EmailId {get;set;}  
  21.         public string Password {get;set;}  
  22.         #endregion  
  23.         public bool Registration1(string id, string pass, string fname, string sname, string lname, string email)   
  24.         {  
  25.             bool res = false;  
  26.             SqlParameter[] sp = new SqlParameter[6];  
  27.             sp[0] = new SqlParameter("@UserId", id);  
  28.             sp[1] = new SqlParameter("@password", pass);  
  29.             sp[2] = new SqlParameter("@fname", fname);  
  30.             sp[3] = new SqlParameter("@sname", sname);  
  31.             sp[4] = new SqlParameter("@lname", lname);  
  32.             sp[5] = new SqlParameter("@email", email);  
  33.             res = objConnection.funSPNonExecuteData("dbo.sp_registration", sp);  
  34.             if (res == true)   
  35.             {  
  36.                 return res;  
  37.             }  
  38.             else  
  39.             {  
  40.                 return false;  
  41.             }  
  42.         }  
  43.     }  
  44. }  
Here is my database structure: 



The following is the Stored Procedure:



After all this is my View.

When creating the view choose strongly typed view and the model as Registration.



And the HTML code for my view is:

  1. @model MVCPROJECT.Models.Registration  
  2. @{  
  3. ViewBag.Title = "Registration";  
  4. }  
  5.   
  6. <div class="panel-heading" style="margin-left:5%;">  
  7.     <h2 class="panel-title" >User Registration Form</h2>  
  8. </div>  
  9. <br />  
  10. <br />  
  11. <div class="panel panel-default" style="margin-left:5%;height:600px">  
  12.   
  13. @using(Html.BeginForm("Registration","Home"))  
  14. {  
  15.   
  16.     <div class="panel-body">  
  17.         <span class="label label-default">First Name</span>  
  18.         <div>  
  19.             <input type="text" class="form-control" placeholder="First Name" name="Fname" aria-describedby="sizing-addon1">  
  20.         </div>  
  21.         <span class="label label-default">Middle Name</span>  
  22.         <div>  
  23.             <input type="text" class="form-control" placeholder="Middle Name" name="sname" aria-describedby="sizing-addon1">  
  24.         </div>  
  25.         <span class="label label-default">Last Name</span>  
  26.         <div>  
  27.             <input type="text" class="form-control" placeholder="Last name" name="lname" aria-describedby="sizing-addon1">  
  28.         </div>  
  29.         <span class="label label-default">User Id</span>  
  30.         <div>  
  31.             <input type="text" class="form-control" placeholder="User Id" name="id" aria-describedby="sizing-addon1">  
  32.         </div>  
  33.         <span class="label label-default">Password</span>  
  34.         <div>  
  35.             <input type="text" class="form-control" placeholder="Password" name="password" aria-describedby="sizing-addon1">  
  36.         </div>  
  37.         <span class="label label-default">Email Id</span>  
  38.         <div>  
  39.             <input type="text" class="form-control" placeholder="Email Id" name="EmailId" aria-describedby="sizing-addon1">  
  40.         </div>  
  41.         <div>  
  42.             <input id="btn_save" type="submit" value="Save" name="Save" />  
  43.             <input id="btn_cancel" type="submit" value="Cancel" name="Cancel" />  
  44.         </div>  
  45.     </div>  
  46. }  
  47.   
  48. </div>  

Note here that the name of the controls should be the same as the the registration model property.

This form will post to the Home control and Registration action methods.

Add a home controller.

Add a Registration action method and write the following code.

The following is my Home controller:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MVCPROJECT.Models;  
  7. namespace MVCPROJECT.Controllers  
  8. {  
  9.     public class HomeController: Controller  
  10.     {  
  11.         Registration reg = new Registration();  
  12.         [HttpGet]  
  13.         public ActionResult Registration()   
  14.         {  
  15.             return View();  
  16.         }  
  17.         [HttpPost]  
  18.         public ActionResult Registration(FormCollection fc, string name)  
  19.         {  
  20.             if (name == "Save"//to check which button is clicked save or cancel  
  21.             {  
  22.                 reg.Id = fc["Id"];  
  23.                 reg.Fname = fc["Fname"];  
  24.                 reg.SName = fc["SName"];  
  25.                 reg.Lname = fc["Lname"];  
  26.                 reg.Password = fc["Password"];  
  27.                 reg.EmailId = fc["EmailId"];  
  28.                 bool res = false;  
  29.                 res = reg.Registration1(reg.Id, reg.Password, reg.Fname, reg.SName, reg.Lname, reg.EmailId);  
  30.                 if (res == true)   
  31.                 {  
  32.                     Response.Write("Registration Done successfully");  
  33.                 }   
  34.                 else   
  35.                 {  
  36.                     Response.Write("Please Try again");  
  37.                 }  
  38.             }   
  39.             else   
  40.             {  
  41.                 Response.Write("data Cancelled");  
  42.             }  
  43.             return View();  
  44.         }  
  45.     }  
  46. }  
Whatever value the user has inserted comes to the controller using the form collection element.
 
From here we retrieve a seperate seperate property and then send it to a method in the model called Registration1. From registration1 it will add the value to the Stored Procedure and execute.

And thus it will start working.



Click Save and check the Registration table.


Similar Articles