Edit And Update Modal In ASP.NET MVC

In the previous article we learned  how to create a business object modal and how to insert data in database. In this article we will learn how to Edit data modal on edit click event. Before proceeding this tutorial will do the following,

create new

Editing Data modal

We want that when we click on EDIT link it should render to the Index Action method of StudentController, with specific ID.

So for that first create an Action method for this and create a view for editing these data.

Action method will be like,

  1. [HttpGet]  
  2. public ActionResult Edit(int ID)  
  3. {  
  4.     Student student = new Student();  
  5.     BusinessLogic BL = new BusinessLogic();  
  6.     Student _student = BL.student.Where(c => c.StudentID == ID).SingleOrDefault();  
  7.     return View(student);  
  8. }  
After that we need for a View , so create a Edit View by right clicking on this action method and adding a view

Add view

Delete the following script from the view:
  1. @section Scripts   
  2. {  
  3.    @Scripts.Render("~/bundles/jqueryval")  
  4. }  
After that View Code will be like following,
  1. @model BusinessLayer.Student  
  2. @  
  3. {  
  4.     ViewBag.Title = "Edit";  
  5. } <  
  6. h2 > Edit < /h2> <  
  7.     div style = "font-family:Arial>  
  8. @using(Html.BeginForm())  
  9.     {  
  10.         @Html.ValidationSummary(true) <  
  11.             fieldset >  
  12.             <  
  13.             legend > Student < /legend>  
  14.         @Html.HiddenFor(model => model.StudentID) <  
  15.             div class = "editor-label" >  
  16.             @Html.LabelFor(model => model.StudentName) <  
  17.             /div> <  
  18.             div class = "editor-field" >  
  19.             @Html.EditorFor(model => model.StudentName)  
  20.         @Html.ValidationMessageFor(model => model.StudentName) <  
  21.             /div> <  
  22.             div class = "editor-label" >  
  23.             @Html.LabelFor(model => model.Gender) <  
  24.             /div> <  
  25.             div class = "editor-field" >  
  26.             @Html.DropDownList("Gender"new List < SelectListItem >  
  27.             {  
  28.                 new SelectListItem  
  29.                 {  
  30.                     Text = "Male", Value = "Male"  
  31.                 },  
  32.                 new SelectListItem  
  33.                 {  
  34.                     Text = "Female", Value = "Female"  
  35.                 }  
  36.             }, "Select Gender") <  
  37.             /div> <  
  38.             div class = "editor-label" >  
  39.             @Html.LabelFor(model => model.StudentClass) <  
  40.             /div> <  
  41.             div class = "editor-field" >  
  42.             @Html.EditorFor(model => model.StudentClass)  
  43.         @Html.ValidationMessageFor(model => model.StudentClass) <  
  44.             /div> <  
  45.             p >  
  46.             <  
  47.             input type = "submit"  
  48.         value = "Save" / >  
  49.             <  
  50.             /p> <  
  51.             /fieldset>  
  52.     } <  
  53.     div >  
  54.         @Html.ActionLink("Back to List""Index") <  
  55.     /div> <  
  56. /div>  
Run your application you will see All the data from database in grid like this,

Index

When we will click on Edit link it will redirect to EDIT View and look like that,

Edit detail

Here you edit the data and click on Save button you will see updated value.

Updating Datamodal

 

    Step 1: Now if we click on Save button it gives error because the edit method is using “[HttpGet]” attribute which means that this action resulting in Edit method is only accepting get request.

    For updating the Edit value in database, first create the stored procedure for updating on, So Stored procedure will be like following,

    Create procedure UpdateStudentInfo,
    1. @StudentID int,  
    2. @StudentName nvarchar(50),  
    3.     @Gender nvarchar(10),  
    4.     @SchoolName nvarchar(50)  
    5. as  
    6. Begin  
    7. Update StudentInfo Set  
    8. StudentName = @StudentName,  
    9.     Gender = @Gender,  
    10.     SchoolName = @SchoolName  
    11. Where StudentID = @StudentID  
    12. End  
    Step 2: Now go to the BussinessLayer Library project and in BusinessLogic.cs class add a save function, For saving the data into databse.
    1. public void SaveStudentInfo(Student student)  
    2. {  
    3.     string connectionString =  
    4.         ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;  
    5.     using(SqlConnection con = new SqlConnection(connectionString))  
    6.     {  
    7.         SqlCommand cmd = new SqlCommand("UpdateStudentInfo", con);  
    8.         cmd.CommandType = CommandType.StoredProcedure;  
    9.         SqlParameter studentId = new SqlParameter();  
    10.         studentId.ParameterName = "@StudentID";  
    11.         studentId.Value = student.StudentID;  
    12.         cmd.Parameters.Add(studentId);  
    13.         SqlParameter studentName = new SqlParameter();  
    14.         studentName.ParameterName = "@StudentName";  
    15.         studentName.Value = student.StudentName;  
    16.         cmd.Parameters.Add(studentName);  
    17.         SqlParameter Gender = new SqlParameter();  
    18.         Gender.ParameterName = "@Gender";  
    19.         Gender.Value = student.Gender;  
    20.         cmd.Parameters.Add(Gender);  
    21.         SqlParameter schoolname = new SqlParameter();  
    22.         schoolname.ParameterName = "@SchoolName";  
    23.         schoolname.Value = student.StudentClass;  
    24.         cmd.Parameters.Add(schoolname);  
    25.         con.Open();  
    26.         cmd.ExecuteNonQuery();  
    27.     }  
    28. }  
    Step 3: Go to Student Controller and add a Edit post method for updating the value. Method code will like following,
    1. [HttpPost]  
    2. public ActionResult Edit()  
    3. {  
    4.     if (ModelState.IsValid)  
    5.     {  
    6.         Student student = new Student();  
    7.         UpdateModel < Student > (student);  
    8.         BusinessLogic BL = new BusinessLogic();  
    9.         BL.SaveStudentInfo(student);  
    10.         return RedirectToAction("Index");  
    11.     }  
    12.     return View();  
    13. }  
    Step 4: Now run your application and click on edit button change the value and save it .. you will see updated value.

    edit

After saving edited value it will look like this:


Similar Articles