Part 80 - mvc 中的 StringLength 属性
来源:互联网 发布:手机键盘映射软件 编辑:程序博客网 时间:2024/06/04 00:31
用法很简单,具体示例如下:
StringLength attribute is present in System.ComponentModel.DataAnnotations namespace and is used to enforce minimum and maximum length of characters that are allowed in a data field. Let's understand this with an example.
We will be using table tblEmployee for this demo.
Create table tblEmployee( Id int primary key identity(1,1), Name nvarchar(50), Email nvarchar(50), Age int, Gender nvarchar(50))Insert into tblEmployee values('Sara Nan', 'Sara.Nani@test.com', 30, 'Female')Insert into tblEmployee values('James Histo', 'James.Histo@test.com', 33, 'Male' )Insert into tblEmployee values('Mary Jane', 'Mary.Jane@test.com', 28, 'Female' )Insert into tblEmployee values('Paul Sensit', 'Paul.Sensit@test.com', 29, 'Male' )Generate ADO.NET entity data model for table tblEmployee. Change the entity name from tblEmployee to Employee. Save and build the project.
Right click on the "Controllers" folder and select Add - Controller. Set
Name = HomeController
Template = MVC controller with read/write actions and views, using Entity Framework
Model class = Employee(MVCDemo.Models)
Data Context Class = EmployeeContext(MVCDemo.Models)
Views = Razor
To validate data, use validation attributes that are found in System.ComponentModel.DataAnnotations namespace. It is not a good idea, to add these validation attributes to the properties of auto-generated "Employee" class, 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 validation attributes. Right click on the "Models" folder and add Employee.cs class file. Copy and paste the following code.
using System.ComponentModel.DataAnnotations;namespace MVCDemo.Models{ [MetadataType(typeof(EmployeeMetaData))] public partial class Employee { } public class EmployeeMetaData { <span style="background-color: rgb(255, 255, 102);">[StringLength(10, MinimumLength = 5)]</span> [Required] public string Name { get; set; } }}Notice that, we have decorated "Name" property with "StringLength" attribute and specified Minimum and Maximum length properties. We also used [Required] attribute. So, at this point Name property is required and should be between 5 and 10 characters.
Points to remember:
1. [StringLength] attribute is present in System.ComponentModel.DataAnnotations namespace.
2. [StringLength] attribute verifies that a string is of certain length, but does not enforce that the property is REQUIRED. If you want to enforce that the property is required use [Required] attribute.
1 0
- Part 80 - mvc 中的 StringLength 属性
- MVC - Part 01
- Part 63 - mvc 分页
- Part 64 - MVC Sorting 排序
- Spring MVC 多选框 绑定 Entity 中的 list 属性
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- 利用AuthorizeAttribute属性避免 MVC 中的跨域攻击
- ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- Asp.Net MVC 视图(概述) - Part.1
- Asp.Net MVC 视图(概述) - Part.1
- Part 5 - Html helpers in mvc
- Part 52 - Partial views in mvc
- Part 60 - ViewStart in asp.net mvc
- Part 62 - mvc search 搜索查询
- Part 86 - ValidationSummary in asp.net mvc
- UVa 1639 Candy
- [CSS]图片无法自适应高宽bug排查过程
- chrome 桌面通知
- 屏幕截图
- 8086汇编-包含多个段的程序
- Part 80 - mvc 中的 StringLength 属性
- 机器学习之决策树--C4.5
- css画企鹅,知识点
- oracle中的sql%found sql%notfound和sql%rowcount
- php 使用smtp发送邮件问题
- mysql计划任务
- 无节操!老婆不听话,当时我就怒了
- TextView + Spanned实现图文混排以及点击交互
- MFC COMBOX 使用