Introduction

In my previous article series I explained how to use scaffolding list, create and edit option. In this article I am going to explain the edit option using scaffolding.

As we know that it is not possible for us to show all column on the screen. So we simply give link of detail and on the click of detail we show the records.

We have following table on our screen with detail button

		
		MVC Scaffolding Detail
		Figure 1
		
		

As we can see detail link with each row. This is actionlink on which we bound the student id. So let’s click on detail link and we will get following link:-

		
		MVC Scaffolding Edit step 2

		Figure 2
		
		

It is showing error because it is searching detail action in Home Controller but as we know that we didn’t define any action for details. You can also see that it is also passing the student id with it. So we have to create action with name detail and we will take id as an input parameter.

Code of detail action is as follows:-

		
		public ActionResult Details(string id)
        {

            return View();
        
        }

		
		

Now create view for this Detail action and also select Detail from scaffolding

		
		MVC Scaffolding Details
	
Figure 3

As you added the view it will give the following code:-

Details.cshtml

		
		@model MvcApplication1.Models.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Details</title>
</head>
<body>
    <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>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id=Model.stu_id }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
</body>
</html>

	
		

As you can see that there is object of model which is of student type. So we have to pass the data from our controller.

So I simply fetched data from database using LINQ and passed this data to this view.

C# Details Code

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


	
		

Now execute this code and you will get following output after clicking on detail link:-

		
		
		MVC Scaffolding detail step 5
		Figure 4
		
		

According to our need we can also customize it. We can also perform Edit functionality from here also.

Email Address

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