http://www.codeproject.com/Articles/775220/ASP-Net-MVC-What-are-the-uses-of-Display-DisplayNa?utm_source=tuicool

In the last blog post on  ASP.Net MVC , we have discussed about implementing ListBoxes. You can read that article  here .
In this article we will go over different display attributes   in  ASP.Net MVC .

Let’s understand this with an example. We will be using tblEmployee table for this. The SQL scripts for creating  tblEmployee  table and inserting data into it are following:

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 

( ‘George Thomas’, ‘Male’ , 37,  ’2014-02-03 16:50:47.788′, ‘GeorgeThomas@BestTEchnologyBlog.com’ , 40000, ‘www.BestTEchnologyBlog.com’ )

Insert into  tblEmployee  values 

( ‘Priyanka’ ,  NULL , 29,  ’2014-03-05 09:53:36.678′, ‘Priyanka@BestTEchnologyBlog.com’ , 36000, ‘www.BestTEchnologyBlog.com’ )

First of all, generate an ADO.NET Entity data model for the table  tblEmployee . You can refer here   to
know the steps to be followed to create an  ADO.NET Entity data model.

Then right click on the  Controllers  folder and add  HomeController .

Include the following  USING  statement to HomeController .

using  MVCDemo.Models;

Copy and paste the following code.

public class   HomeController  :  Controller 

{

 public  ActionResult  Details( int  id) 

{ 

SampleDBContext  db =  new  SampleDBContext (); 

tblEmployee  employee = db.tblEmployees.Single(x => x.Id == id); 

return  View(employee); 

}

}

Then right click on the  Details  action method and add  Details  view. Make sure that you are creating a strongly typed view against tblEmployee  class. Select Details  as the  Scaffold
template
 .

Set Aerial as our font – family by using a div tag.

Build the Solution and run it. We will get a screen like below.

But look at the output , it is not much pretty. There is no space in between Full andName and is displaying as FullName. Gender  is showing as blank. We have to make it much more pretty. The text should be Full
Name
 instead of FullName and ifGender is not specified, instead of showing blank there, a text of Gender not specified  should be appeared. How can we achieve this? Here comes the importance of display
attributes.

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 tblEmployeeclass 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. Notice that I have tried to include the purpose of each attribute through the comments. Please read them carefully.

namespace  MVCDemo.Models 

{ 

    [ MetadataType ( typeof ( EmployeeMetaData ))] 

     public partial class   tblEmployee 

    { 

    }

     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  }")] 

        //[DisplayFormatAttribute(DataFormatString="{0  }")] 



        //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;  } 

    } 

}

Don’t forget to include following using statements:

using  System.ComponentModel.DataAnnotations; 

using  System.ComponentModel;

Now build the solution and run it. We can see a page like below.

Here everything is OK except the Salary. Even if we have used  [ ScaffoldColumn (false )] attribute for the Salary, it is still showing. I think you can guess the reason. In the comments itself, I have specified that  ScaffoldColumn  attribute
will work only when we use @Html.DisplayForModel() helper.

So instead of all the HTML in the View, we will get the same output by just adding one line of code which is shown below.

@Html.DisplayForModel()

This HTML helper will go through each property and will render the UI automatically.

Now let’s build the solution by pressing Ctrl+Shift+B  and refresh the page. We can see that the Salary is now hidden.

ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes的更多相关文章

  1. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  2. 使用Visual Studio 2015 开发ASP.NET MVC 5 项目部署到Mono/Jexus

    最新的Mono 4.4已经支持运行asp.net mvc5项目,有的同学听了这句话就兴高采烈的拿起Visual Studio 2015创建了一个mvc 5的项目,然后部署到Mono上,浏览下发现一堆错 ...

  3. 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino

    大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...

  4. Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合

    今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...

  5. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

  6. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之目录导航

    ASP.NET MVC with Entity Framework and CSS是2016年出版的一本比较新的.关于ASP.NET MVC.EF以及CSS技术的图书,我将尝试着翻译本书以供日后查阅. ...

  7. ASP.NET MVC开发:Web项目开发必备知识点

    最近加班加点完成一个Web项目,使用Asp.net MVC开发.很久以前接触的Asp.net开发还是Aspx形式,什么Razor引擎,什么MVC还是这次开发才明白,可以算是新手. 对新手而言,那进行A ...

  8. ASP.NET MVC原理

    仅此一文让你明白ASP.NET MVC原理   ASP.NET MVC由以下两个核心组成部分构成: 一个名为UrlRoutingModule的自定义HttpModule,用来解析Controller与 ...

  9. ASP.NET MVC——模型绑定

    这篇文章我们来讲讲模型绑定(Model Binding),其实在初步了解ASP.NET MVC之后,大家可能都会产生一个疑问,为什么URL片段最后会转换为例如int型或者其他类型的参数呢?这里就不得不 ...

随机推荐

  1. Nginx实现负载均衡 + Keepalived实现Nginx的高可用

    前言 使用集群是大中型网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求 ...

  2. flash as3.0学习笔记

    F9开动作模板 trace输出 trace(a); 影片剪辑 var mc:MovieClip = new MovieClip();//属性(x,y轴)方法 play,stop mc.x = 10 / ...

  3. Windows系统开发常用类-------------Environment类

    Windows系统开发常用类-------------Environment类:         SystemDirectory//显示系统目录         MachineName//计算机名称 ...

  4. 修改数组数据头和尾push()、pop()和unshift()、shift()

    1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及内容. 不同的是 push().pop() 是从数组的尾部进行增减,unshift ...

  5. 8) 十分钟学会android--Activity的生命周期之停止与重启

    恰当的停止与重启我们的activity是很重要的,在activity生命周期中,他们能确保用户感知到程序的存在并不会丢失他们的进度.在下面一些关键的场景中会涉及到停止与重启: 用户打开最近使用app的 ...

  6. root密码忘记怎么办?

    忘记root密码:按 e进入内核在按e,后面加1 .按b启动 进入命令行输入passwd,设置新的密码后exit退出即可

  7. Vmware安装Linux系统的vmware-tools

    我们应对都用vmware虚拟机,如果虚拟机是windows系统,每次虚拟机下方提示安装vmware-tools时,我们只需要点击安装即可. 但如果虚拟机安装的是linux系统的时候,发现就没一样了. ...

  8. Nginx下修改php.ini后重新加载配置文件命令

    修改php.ini后 如,我的 php.ini 文件是放在 /etc/php.ini php 所在目录是 /www/Linux/php-5.2.17 修改 php.ini 后要用 php-fpm 来进 ...

  9. 设计模式(C++实现)--一句话总结

    原文链接:http://blog.csdn.net/LCL_data/article/details/12117349 按照目的来分,设计模式可以分为创建型模式.结构型模式和行为型模式. 按照目的来分 ...

  10. mongodb主从副本集配置

    创建路径: mkdir -p /datassd/mongo_20011/{data,conf,log}     配置文件示例: #mongo.conf dbpath=/datassd/mongo/da ...