ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes
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的更多相关文章
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- 使用Visual Studio 2015 开发ASP.NET MVC 5 项目部署到Mono/Jexus
最新的Mono 4.4已经支持运行asp.net mvc5项目,有的同学听了这句话就兴高采烈的拿起Visual Studio 2015创建了一个mvc 5的项目,然后部署到Mono上,浏览下发现一堆错 ...
- 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino
大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...
- Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合
今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之目录导航
ASP.NET MVC with Entity Framework and CSS是2016年出版的一本比较新的.关于ASP.NET MVC.EF以及CSS技术的图书,我将尝试着翻译本书以供日后查阅. ...
- ASP.NET MVC开发:Web项目开发必备知识点
最近加班加点完成一个Web项目,使用Asp.net MVC开发.很久以前接触的Asp.net开发还是Aspx形式,什么Razor引擎,什么MVC还是这次开发才明白,可以算是新手. 对新手而言,那进行A ...
- ASP.NET MVC原理
仅此一文让你明白ASP.NET MVC原理 ASP.NET MVC由以下两个核心组成部分构成: 一个名为UrlRoutingModule的自定义HttpModule,用来解析Controller与 ...
- ASP.NET MVC——模型绑定
这篇文章我们来讲讲模型绑定(Model Binding),其实在初步了解ASP.NET MVC之后,大家可能都会产生一个疑问,为什么URL片段最后会转换为例如int型或者其他类型的参数呢?这里就不得不 ...
随机推荐
- oc2---类
// main.m // 第一个OC类,OC中的类其实本质就是一个结构体, 所以p这个指针其实就是指向了一个结构体,创建一个对象就是创建一个结构体指针, #import <Foundation/ ...
- angular里使用vue/vue组件怎么在angular里用
欢迎加入前端交流群交流知识&&获取视频资料:749539640 如何在angularjs(1)中使用vue参考: https://medium.com/@graphicbeacon/h ...
- [TB-Technology] 淘宝在数据处理领域的项目及开源产品介绍
淘宝在数据存储和处理领域在国内互联网公司中一直保持比较靠前的位置,而且由于电子商务领域独特的应用场景,淘宝在数据实时性和大规模计算及挖掘方面一直在国内保持着领先,因此积累了很多的实践的经验和产品. T ...
- vue插件 vue-seamless-scroll 无缝滚动插件ES6使用总结
最近因为需求需要写一个项目信息无缝向上滚动组件,在网上搜了一下,看到大家的一致好评就果断的使用了vue-seamless-scroll组件.下面就简单的介绍它的使用,具体详细的使用推荐大家去看下开发者 ...
- js两个页面之间URL传递参数中文乱码
- [Offer收割]编程练习赛34
共同富裕 显然每次选最大的数字,其余的加一.也可以理解为每次选一个最大的数字减一,直到所有数字都变成最小的数字为止. #include<stdio.h> #include<strin ...
- Java程序连接各种数据库的driver和url形式
1.Oracle数据库 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url = & ...
- Java 方法的应用
Java方法可以理解为C#中的函数,都是把复杂的问题简单化,按模块,按功能区分,分别完成各个部分在调用这些方法完成整个功能. 方法的综合练习,猜数字的实现: 代码要求: 生成不重复的4位数字(只有1- ...
- VC++抛出自定义编译期异常的指令
#error 即可, 抛出消息是 #pragma message 最新的还有static_assert有一些用 一下子忘了网上居然搜不到...尝试了 关键字vc++.vc.vs.msvc + 抛出编 ...
- 给DBA 的mysql脚本格式