Introduction To Editing Application in MVC5

Introduction

In this article, I am introducing you to adding information in Edit, Details and Delete links. As I described in my earlier article of how to add a new property. So now in here you will learn to add information related to the property in an Edit link, Details link and Delete link.

In that context, we need to change some code in the following files:

  • Edit.cshtml file
  • Details.cshtml file
  • Delete.cshtml file

Update in Edit link

As you can see in the following image that the new property named Grade that is present in the Index page, isn't present when the user clicks on the Edit link, because we didn't change the Edit.cshtml file.

Index-in-MVC5.jpg

Click on the Edit link.

Edit-in-MVC5.jpg

So, let's start with the following procedure.

Step 1: Select the Edit.cshtml file from the Solution Explorer.

edit.jpg

Step 2: Change your code with the following code. I have added some extra code in my previous code:

  1. <h2>Edit</h2>  
  2. @using (Html.BeginForm()) {  
  3.     @Html.AntiForgeryToken()  
  4.     @Html.ValidationSummary(true)  
  5.     <fieldset class="form-horizontal">  
  6.         <legend>Cricketer</legend>  
  7.         @Html.HiddenFor(model => model.ID)  
  8.         <div class="control-group">  
  9.             @Html.LabelFor(model => model.Name, new { @class = "control-label" })  
  10.                      <div class="controls">  
  11.                            @Html.EditorFor(model => model.Name)  
  12.                            @Html.ValidationMessageFor(model => model.Name, nullnew { @class = "help-inline" })  
  13.                      </div>  
  14.               </div>  
  15.         <div class="control-group">  
  16.             @Html.LabelFor(model => model.ODI, new { @class = "control-label" })  
  17.                      <div class="controls">  
  18.                            @Html.EditorFor(model => model.ODI)  
  19.                            @Html.ValidationMessageFor(model => model.ODI, nullnew { @class = "help-inline" })  
  20.                      </div>  
  21.               </div>  
  22.         <div class="control-group">  
  23.             @Html.LabelFor(model => model.Test, new { @class = "control-label" })  
  24.                      <div class="controls">  
  25.                            @Html.EditorFor(model => model.Test)  
  26.                            @Html.ValidationMessageFor(model => model.Test, nullnew { @class = "help-inline" })  
  27.                      </div>  
  28.               </div>  
  29.         <div class="control-group">  
  30.             @Html.LabelFor(model => model.Grade, new {@class = "control-label" })  
  31.             <div class="controls">  
  32.                 @Html.EditorFor(model=>model.Grade)  
  33.                 @Html.ValidationMessageFor(model => model.Grade, nullnew { @class="help-inline" })  
  34.             </div>  
  35.         </div>  
  36.         <div class="form-actions no-color">  
  37.             <input type="submit" value="Save" class="btn" />  
  38.         </div>  
  39.     </fieldset>  
  40. } 

Step 3: Debug your application.

NewIndex-in-MVC5.jpg

Step 4: Click on the Edit link.

NewEdit-in-MVC5.jpg

You can now see that the Grade field is present on the Edit Page.

Update in Details link

As you can see in the following image, the new property named Grade in the Index page, isn't present when a user clicks on the Details link, because we didn't change the Details.cshtml file.

IndexDetails-in-MVC5.jpg

Click on the Details link.

Details-in-MVC5.jpg

So, let's start with the following procedure.

Step 1: Select the Details.cshtml file from the Solution Explorer.

DetailsSolutionexplorer.jpg

Step 2: Change your code with the following code. I have added some extra code in my previous code:

  1. <h2>Details</h2>  
  2. <fieldset>  
  3.        <legend>Cricketer</legend>  
  4.        <dl>  
  5.               <dt>  
  6.                      @Html.DisplayNameFor(model => model.Name)  
  7.               </dt>  
  8.               <dd>  
  9.                      @Html.DisplayFor(model => model.Name)  
  10.               </dd>  
  11.               <dt>  
  12.                      @Html.DisplayNameFor(model => model.ODI)  
  13.               </dt>  
  14.               <dd>  
  15.                      @Html.DisplayFor(model => model.ODI)  
  16.               </dd>  
  17.               <dt>  
  18.                      @Html.DisplayNameFor(model => model.Test)  
  19.               </dt>  
  20.               <dd>  
  21.                      @Html.DisplayFor(model => model.Test)  
  22.               </dd>  
  23.         <dt>  
  24.             @Html.DisplayNameFor(model => model.Grade)  
  25.         </dt>  
  26.         <dd>  
  27.             @Html.DisplayFor(model => model.Grade)  
  28.         </dd>  
  29.        </dl>  
  30. </fieldset>  

Step 3: Debug your application.

IndexDetails-in-MVC5.jpg

Step 4: Click on the Details link.

NewDetails-in-MVC5.jpg

You can now see that the Grade field is present on the Details Page.

Update in Delete link

As you can see in the following image, the new property named Grade in the Index page, isn't present when a user clicks on the Delete link, because we didn't change the Delete.cshtml file.

IndexDelete-in-MVC5.jpg

Click on the Delete link.

Delete-in-MVC5.jpg

So, let's start with the following procedure.

Step 1: Select the Delete.cshtml file from the Solution Explorer

DeleteSolutionExplorer.jpg

Step 2: Change your code with the following code. I have added some extra code in my previous code:

  1. <h3>Are you sure you want to delete this?</h3>  
  2. <fieldset>  
  3.     <legend>Cricketer</legend>  
  4.        <dl>  
  5.               <dt>  
  6.                      @Html.DisplayNameFor(model => model.Name)  
  7.               </dt>  
  8.               <dd>  
  9.                      @Html.DisplayFor(model => model.Name)  
  10.               </dd>  
  11.               <dt>  
  12.                      @Html.DisplayNameFor(model => model.ODI)  
  13.               </dt>  
  14.               <dd>  
  15.                      @Html.DisplayFor(model => model.ODI)  
  16.               </dd>  
  17.               <dt>  
  18.                      @Html.DisplayNameFor(model => model.Test)  
  19.               </dt>  
  20.               <dd>  
  21.                      @Html.DisplayFor(model => model.Test)  
  22.               </dd>  
  23.         <dt>  
  24.             @Html.DisplayNameFor(model=>model.Grade)  
  25.         </dt>  
  26.         <dd>  
  27.             @Html.DisplayFor(model=>model.Grade)  
  28.         </dd>  
  29.        </dl>  
  30. </fieldset>   

Step 3: Debug your application.

IndexDelete-in-MVC5.jpg

Step 4: Click on the Delete link.

NewDelete-in-MVC5.jpg

You can now see that the Grade field is present on the Delete Page.

Summary

This article will help you to add information in Edit, Details and Delete pages because the new property that has been added in the Create page is not present in the Edit, Details and Delete page. I didn't change the Edit.cshtml, Details.cshtml and Delete.cshtml files previously. Now, you have the complete updated ASP.NET MVC Application which will definitely help you.

Thanks for reading my article.


Similar Articles