针对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. Python 学习笔记三

    笔记三:函数 笔记二已取消置顶链接地址:http://www.cnblogs.com/dzzy/p/5289186.html 函数的作用: 給代码段命名,就像变量給数字命名一样 可以接收参数,像arg ...

  2. BZOJ 3445: [Usaco2014 Feb] Roadblock

    Description 一个图, \(n\) 个点 \(m\) 条边,求将一条边距离翻倍后使 \(1-n\) 最短路径增加的最大增量. Sol Dijstra. 先跑一边最短路,然后枚举最短路,将路径 ...

  3. PHP输出控制(Output Control)函数

    ob_start 此函数将打开输出缓冲.当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中. 内部缓冲区的内容可以用 ob_get_contents() ...

  4. idea修改默认快捷键

    点击file ,选择settings. 输入keymap: 因为多数人使用的都是eclipse,比较容易上手,习惯了eclipse的键位,如 此就能更换. 也可以在对应的操作上,设置自己熟悉的键位.

  5. 【转】【编码】ASCII 、UNICODE和UTF-8之二

    字符发展 1. 美国 ASCII-(American standard code information interchange) 美国信息互换标准代码 范围:1-127 ; 单字 备注:前部用作控制 ...

  6. tomcat 访问软连接

    Linux创建软连接: ln -s 源文件 目标文件 tomcat安装目录 / conf目录下的:context.xml文件在 <Context />; 里面加上 allowLinking ...

  7. EXTJS Ajax请求中文乱码

    设置actionMethods, read为post proxy: { type: 'ajax', url: '../Dictionary/query', actionMethods: { read: ...

  8. docker ui

    docker run -d -p 9000:9000 --privileged -v /var/run/docker.sock:/var/run/docker.sock uifd/ui-for-doc ...

  9. JS插件之——ztree

    很牛逼的一个树形菜单,树形下拉框插件.一年前用过,很好用.今天又有机会拿过来用,温故一下基本点,nice!! 官方文档说明的非常详细,按照API慢慢看,耐心解读,自然就可以解惑了. 官方文档及其源码下 ...

  10. Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...