DataType,DisplayFormate
在Models->SysUser.cs中
添加 public DateTime CreateDate { get; set; }
在Views\Account\Index.cshtml,把創建日期顯示出來
<thead>
<tr>
<th>
@Html.ActionLink("UserName", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter})
</th>
<th>
Email
</th>
<th>
CreateDate
</th>
在foreach中添加
<td>
@Html.DisplayFor(modelItem=>item.Department.Name)
</td>
默認顯示的日期顯示到具體時間而我們需要的是顯示年月日就行了
在Models->SysUser.cs中添加
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="{0:yyyy-MM-dd}",ApplyFormatInEditMode=true)]
{0:yyyy-MM-dd}:顯示的具體格式
StringLength屬性
StringLength屬性設置了數據庫中存儲字段的最大長度,為程序提供客戶端和服務器端的驗證。同樣用這個屬性也可以指定最小長度,不過不影響數據庫的結構。
在models->SysUser.cs中添加
[StringLength (10,ErrorMessage ="名字不能超過10個字。")]
當輸入超過十個字符是會提示錯誤
Column屬性
有時會有這么一種情況,我們Model中的字段和數據庫中表的字段要用不同的命名。例如我們Model中命名為UserName,數據庫表中命名為LoginName.
在models->SysUser.cs中添加
[Column("LoginName")]
可以將多個屬性寫在一塊用逗號隔開,例如
[Column("FirstName"),Display(Name = "First Name"),StringLength(50, MinimumLength=1)]
結合asp.net里面的屬性可以更好的理解。