Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 40 - Using displayname, displayformat, scaffoldcolumn attributes in asp.net mvc application

Suggested Videos 
Part 37 - Generating a radiobuttonlist control in mvc using HTML helpers
Part 38 - CheckBoxList in asp.net mvc
Part 39 - ListBox in asp.net mvc

In this video, we will discuss using the following attributes, with examples. 
1. Display
2. DisplayName
3. DisplayFormat
4. ScaffoldColumn



We will be using table tblEmployee for this demo. 
Create table tblEmployee
(
Id int primary key identity,
FullName nvarchar(100),
Gender nvarchar(10),
Age int,
HireDate DateTime,
EmailAddress nvarchar(100),
Salary int,
PersonalWebSite nvarchar(100)
)



Insert into tblEmployee values
('John Smith', 'Male', 35, '2007-01-02 17:53:46.833', 'JohnSmith@pragimtech.com', 45000, 'http://www.pragimtech.com')
Insert into tblEmployee values
('Mary Jane', NULL, 30, '2009-05-02 19:43:25.965', 'MaryJane@pragimtech.com', 35000, 'http://www.pragimtech.com')

Generate ADO.NET entity data model for table tblEmployee. Change the entity name from tblEmployee to Employee. Save and build the project.

Right click on the "Controllers" folder and add "HomeController". Include the following "USING" statement.
using MVCDemo.Models;

Copy and paste the following code.
public class HomeController : Controller
{
    public ActionResult Details(int id)
    {
        SampleDBContext db = new SampleDBContext();
        Employee employee = db.Employees.Single(x => x.Id == id);
        return View(employee);
    }
}

Right click on the "Details" action method, and add "Details" view. Make sure you are creating a strongly typed view against "Employee" class. Select "Details" as the "Scaffold Template". Run the application and notice that, the output is not that pretty.

We can control the display of data in a view using display attributes that are found in System.ComponentModel.DataAnnotations namespace. It is not a good idea, to add display attributes to the properties of auto-generated "Employee" class, as our changes will be lost, if the class is auto-generated again.

So, let's create another partial "Employee" class, and decorate that class with the display attributes.  Right click on the "Models" folder and add Employee.cs class file. Copy and paste the following code.
namespace MVCDemo.Models
{
    [MetadataType(typeof(EmployeeMetaData))]
    public partial class Employee
    {
    }

    public class EmployeeMetaData
    {
        //If you want "FullName" to be displayed as "Full Name", 
        //use DisplayAttribute or DisplayName attribute.
        //DisplayName attribute is in System.ComponentModel namespace.
        //[DisplayAttribute(Name="Full Name")]
        //[Display(Name = "Full Name")]
        [DisplayName("Full Name")]
        public string FullName { get; set; }

        //To get only the date part in a datetime data type
        //[DisplayFormat(DataFormatString = "{0:d}")]
        //[DisplayFormatAttribute(DataFormatString="{0:d}")]

        //To get time in 24 hour notation
        //[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}")]

        //To get time in 12 hour notation with AM PM
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
        public DateTime? HireDate { get; set; }

        // If gender is NULL, "Gender not specified" text will be displayed.
        [DisplayFormat(NullDisplayText = "Gender not specified")]
        public string Gender { get; set; }

        //If you don't want to display a column use ScaffoldColumn attribute.
        //This only works when you use @Html.DisplayForModel() helper
        [ScaffoldColumn(false)]
        public int? Salary { get; set; }
    }
}

Make sure to include the following using statements:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

We will discuss the following attributes in our next video session.
DataTypeAttribute, 
DisplayColumnAttribute

9 comments:

  1. i really like the comments in the code, i can more easily remind myself what it means after few days.

    ReplyDelete
  2. Very useful for a MVC newer... That is best I have ever seen

    ReplyDelete
  3. Excellent videos with clear and useful instructions instead of covering aspects which you will probably never use. I am very grateful for these videos and appreciate all of the time and effort you put into creating them. Best ones I have ever viewed and I have been a developer for over 10 years. J Perez

    ReplyDelete
  4. Difference between display, displayname, displayattribute

    ReplyDelete
  5. I am using Entity frame work 5.0 version and asp.net of 2012 and for this tutorial when i am creating employee.cs class it saying that its already there.. In model folder already that class is there which we can edit but the caution is like don't make any changes what should i do for metada data information editing..?????

    ReplyDelete
    Replies
    1. Create with different name. But internally change class name as employee.

      Delete
  6. Absolutely the best MVC tutorials I have come across. Thank you.

    ReplyDelete
  7. I am working with MVC 5 but when i am updating model from database then my validation changes vanished from class file and i can't make any other partial class in models folder so now what is the solution. Please help to find solution.

    ReplyDelete
  8. If I saw these in my Collage time , I would be best Programmer.

    ReplyDelete

It would be great if you can help share these free resources