Introduction

In my last article series I explained scaffolding LIST, Create, Edit and Detail option. In this article I am going to explain Delete option.

As you see when we execute this code we will get following window where we get delete option.

		
		MVC Scaffolding Detail
		Figure 1
		
		

As you click on the delete button you will get following error:-

		
		MVC Scaffolding Edit step 2

		Figure 2
		
		

It is simply searching Delete action and it is passing id of student. So first we have to create action for Delete with id as parameter.

		
		public ActionResult Delete(string id)
        {
            return View();
        
        }

		
		

Now add the view for this Delete Action

Delete.cshtml

		
		@model MvcApplication1.Models.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Delete</title>
</head>
<body>
    <h3>Are you sure you want to delete this?</h3>
    <fieldset>
        <legend>Student</legend>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.stu_name)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.stu_name)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.stu_age)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.stu_age)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.course)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.course)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.fees)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.fees)
        </div>
    </fieldset>
    @using (Html.BeginForm()) {
        <p>
            <input type="submit" value="Delete" /> |
            @Html.ActionLink("Back to List", "Index")
        </p>
    }

</body>
</html>
	
		

As you can see that on the top it is taking object of model i.e. student.

So we have to pass data from controller

C# Delete Code

		
		public ActionResult Delete(string id)
        {
            MvcApplication1.Models.Student stu = dc.GetTable<MvcApplication1.Models.Student>().Single(x => x.stu_id == Convert.ToInt32(id));
            return View(stu);
        
        }
	
		

So when you execute this code and click on delete button and you will get following window:-

		
		
		MVC Scaffolding detail step 5
		Figure 3
		
		

As you can see that it is showing records and there is delete button here. When we click on delete button it will simply fire the Delete action again. So we have to create one more action for Delete. But there will be two delete on the same page with same parameter so differentiate them we changed the method name add ActionName tag on it.

Controller Code for Delete

		
		[HttpGet]
        [ActionName("Delete")]
        public ActionResult Delete_Get(string id)
        {
            MvcApplication1.Models.Student stu = dc.GetTable<MvcApplication1.Models.Student>().Single(x => x.stu_id == Convert.ToInt32(id));
            return View(stu);
        
        }
        [HttpPost]
        [ActionName("Delete")]
        public ActionResult Delete_Post(string id)
        {
          MvcApplication1.Models.Student stu = dc.GetTable<MvcApplication1.Models.Student>().Single(x => x.stu_id == Convert.ToInt32(id));
          dc.GetTable<MvcApplication1.Models.Student>().DeleteOnSubmit(stu);
          dc.SubmitChanges();
            return View();

        }
	
		

Now execute this code and click on delete button and after that check the list and you will find your data deleted.

		
		
		MVC Scaffolding detail step 5
		Figure 4
		
		

After pressing delete button check the list

		
		
		MVC Scaffolding detail step 5
		Figure 5
		
		

In my next article series I will also discuss these features in more efficiently.

But these scaffolding series is only to interact with scaffolding using LINQ

Email Address

For any query you can send mail at info@techaltum.com
Thanks