针对MVC中实例类型的修饰,我们可以采用DataAnnotations类来完成,该类所处的命名空间是System.ComponentModel.DataAnnotations;

一、通过开类型,能够修改Model类型的属性,如下代码所示:

public class Employee
{
[Key]
public int EmployeeId { get; set; } [Required(ErrorMessage = "Enter First Name"), StringLength()]
public string FirstName { get; set; } [StringLength(,ErrorMessage="Last Name length should be greater than 50")]
public string LastName { get; set; } public int Salary { get; set; }
}

备注:

1、[key]主键唯一,Required必填,ErrorMessage错误提示内容,StringLength表述字段长度。

Form表单代码:

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>CreateEmployee</title>
</head>
<body>
<div>
<form action="/Employee/SaveEmployee" method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" id="TxtFName" name="FirstName" value="" /></td>
<td>@Html.ValidationMessage("FirstName")</td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" id="TxtLName" name="LastName" value="" /></td>
<td>@Html.ValidationMessage("LastName")</td>
</tr>
<tr>
<td>Salary:</td>
<td><input type="text" id="TxtSalary" name="Salary" value="" /></td>
<td>@Html.ValidationMessage("Salary")</td>
</tr>
<tr>
<td>
<input type="submit" name="BtnSave" value="Save Employee" />
<input type="submit" name="BtnSave" value="Cancel" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

备注:@Html.ValidationMessage表述验收字段异常时,显示异常提示信息。

后台Action代码:

      public ActionResult SaveEmployee(Employee et, string BtnSave)
{
switch (BtnSave)
{
case "Save Employee":
if (ModelState.IsValid)
{
EmployeeBusinessLayer empbal = new EmployeeBusinessLayer();
empbal.SaveEmployee(et);
return RedirectToAction("Index");
}
else
{
return View("CreateEmployee");
}
case "Cancel":
// RedirectToAction("index");
return RedirectToAction("Index");
}
return new EmptyResult();
}

备注:ModelState.IsValid表述Model字段验证是否有效

二、自定义服务端验证

1、新建自定义类:

public class FirstNameValidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
{
return new ValidationResult("Please Provide First Name");
}
else
{
if (value.ToString().Contains("@"))
{
return new ValidationResult("First Name should not contain @");
}
}
return ValidationResult.Success;
}
}

2、使用自定义验证类型

 public class Employee
{
[Key]
public int EmployeeId { get; set; } [Required(ErrorMessage = "Enter First Name"), StringLength(), FirstNameValidation]
public string FirstName { get; set; } public int Salary { get; set; }
}

MVC中数据的内部校验的更多相关文章

  1. MVC中数据验证

    http://www.studyofnet.com/news/339.html http://www.cnblogs.com/kissdodog/archive/2013/05/04/3060278. ...

  2. 二、Ajax请求MVC中数据查询表返回datatable

    一.Ajax请求MVC中数据查询表返回datatable 解决方式 返回list

  3. MVC中数据传递 ViewBag的使用

    ViewBag MVC3中 ViewBag.ViewData和TempData的使用和差别 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC3中保留 ...

  4. Asp.net mvc 中Action 方法的执行(二)

    [toc] 前面介绍了 Action 执行过程中的几个基本的组件,这里介绍 Action 方法的参数绑定. 数据来源 为 Action 方法提供参数绑定的原始数据来源于当前的 Http 请求,可能包含 ...

  5. MVC中的数据注解和验证

    数据注解和验证 用户输入验证在客户端浏览器中需要执行验证逻辑. 在客户端也需要执行. 注解是一种通用机制, 可以用来向框架注入元数据, 同时, 框架不只驱动元数据的验证, 还可以在生成显示和编辑模型的 ...

  6. Asp.net MVC中 Controller 与 View之间的数据传递

    在ASP.NET MVC中,经常会在Controller与View之间传递数据 1.Controller向View中传递数据 (1)使用ViewData["user"] (2)使用 ...

  7. ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)

    http://www.cnblogs.com/zyqgold/archive/2010/11/22/1884779.html 在ASP.NET MVC框架中,将视图中的数据传递到控制器中,主要通过发送 ...

  8. 如何在.NET MVC中使用jQuery并返回JSON数据

    http://blog.csdn.net/dingxingmei/article/details/9092249 开始实践 - jQuery端 假设我们要从服务器端获取一个文章列表,并把文章条目显示在 ...

  9. MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结

    一.引言 MVC中view向controller传递数据的时候真心是一个挺让人头疼的一件事情.因为原理不是很懂只看一写Dome,按葫芦画瓢只能理解三分吧. 二.解读Jquery个Ajax函数 $.ge ...

随机推荐

  1. 插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法

    SQL Server 2008 R2 x64 下 事情是这样的, 使用Navcate的数据导出备份工具导出表的记录 INSERT INTO [xxx_users] VALUES (1, 'tom ', ...

  2. Zebra_Form Packages: Zebra_Form Controls Generic XSS_Clean Classes: Zebra_Form_Control Class: Zebra_Form_Control

    http://stefangabos.ro/wp-content/docs/Zebra_Form/Generic/Zebra_Form_Control.html#methodset_rule

  3. MY_FIRSH_MODULE

    模块描述 将30个字节的内存空间模仿成设备文件.每次读写不超过30个字节. 模块加载成功之后,需建立设备文件 mknod /dev/mydev c 231 0 模块代码 #include <li ...

  4. iOS开发之loadView和viewDidLoad总结

    http://www.1000phone.net/forum.php?mod=viewthread&tid=10507&extra=page%3D1%26filter%3Dauthor ...

  5. jQuery工作原理

    jQuery的开篇声明里有一段非常重要的话:jQuery是为了改变javascript的编码方式而设计的.从这段话可以看出jQuery本身并不是UI组件库或其他的一般AJAX类库.jQuery改变ja ...

  6. 套接字Socket

    TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设计的.UDP( ...

  7. (转) Lambda表达式中的表达式lambda和语句lambda区别

    Lambda表达式可分为表达式lambda和语句lambda 表达式lambda:表达式位于 => 运算符右侧的lambda表达式称为表达式lambda (input parameters) = ...

  8. windows下安装Apache 64bit

    文件下载:http://pan.baidu.com/s/1c0oDjFE 一.Apache的安装 http://www.blogjava.net/greatyuqing/archive/2013/02 ...

  9. andrond mk通配符遍历文件夹

    define all-cpp-files-under$(patsubst ./%,%, \ $(shell cd $(LOCAL_PATH) ; \ find $(1) -name "*.c ...

  10. Storm自带测试案例的运行

    之前Storm安装之后,也知道了Storm的一些相关概念,那么怎么样才可以运行一个例子对Storm流式计算有一个感性的认识呢,那么下面来运行一个Storm安装目录自带的测试案例,我们的Storm安装在 ...