zoukankan      html  css  js  c++  java
  • 添加 Attribute(属性)和Attribute的用法

    DataType,DisplayFormate

    在Models->SysUser.cs中

    添加 public DateTime CreateDate { get; set; }

    在ViewsAccountIndex.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里面的属性可以更好的理解。

  • 相关阅读:
    iperf/iperf3网络测试工具的安装与使用
    驱动模块(4)——模块编译
    760. Find Anagram Mappings
    MySQL面试题
    MySQL 的数据存储引擎
    203. Remove Linked List Elements
    数据库事务隔离级别
    232. Implement Queue using Stacks
    MySQL中的事务
    482. License Key Formatting
  • 原文地址:https://www.cnblogs.com/wwr01/p/7795114.html
Copyright © 2011-2022 走看看