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. Ubuntu Tomcat Service

    只需要将%TOMCAT_HOME%/bin/catalina.sh文件拷贝到/etc/init.d/文件夹下,稍作编辑,然后注册成系统服务,是否设置自启动均可. 1. 编辑catalina.sh文件c ...

  2. oc21--super

    // // Phone.h #import <Foundation/Foundation.h> typedef enum { kFlahlightStatusOpen, kFlahligh ...

  3. APP_ABI

    在Application.mk文件中有个预定义命令参数APP_ABI,是指明编译与调试的CPU架构. 目前Android系统支持以下七种不同的CPU架构:ARMv5,ARMv7(从2010年起),x8 ...

  4. 第8章 MyBatis简介

    # 创建一个名称为mybatis的数据库 CREATE DATABASE mybatis; # 使用名称为mybatis的数据库 USE mybatis; # 创建一个tb_user表,有id.nam ...

  5. canvas的自动画图

    <!DOCTYPE HTML><html><body> <canvas id="myCanvas" width="200&quo ...

  6. 升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链手机分拣系统使用说明_升鲜宝生鲜供应链管理系统_15382353715_余东升

    1. 系统概述 升鲜宝供应链手机分拣系统(Android版)是升鲜宝供应链管理系统的配套分拣系统,本系统适用于农副产品生鲜配送供应链企业分拣过程,在线上数据管理和线下业务管理两大方面提供解决方案,该系 ...

  7. FlappyBird模拟(不完整版本)

    FlappyBird模拟(不完整版本) 准备材料 land地 sky天 pipe管道 bird小鸟 Land.js function Land(info) { this.x = info.x; thi ...

  8. JavaScript的continue和break的区别

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  9. hdu3488 / hdu3435 / hdu1853 最小费用最大流 圈 拆点

    题目大意: 在一个有向图中,求经过所有点的最小圈. 思路: (如果是用二分图的完美匹配来做,那么直接上模版就好了).http://www.cnblogs.com/Potato-lover/p/3991 ...

  10. 第五周课后作业——适用于人工智能的visual studio 的创新分析

    个人觉得作业布置的(2)(3)(4)(5)的顺序并不合理,我将以(5)(2)(3)(4)的顺序开展我的分析. 创新的定义是做出一些改变或创造出新的东西,既是过程,也是结果.这是一个很泛的概念,所以去问 ...