MVC客户端验证的小示例

配置客户端验证的可用性:

<configuration>
 <appSettings>
  <add key="ClientValidationEnabled" value="true"/>
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
 </appSettings>
</configuration>

MVC的客户端的验证也利用了实体上的特性标签,如下:

public class Auction
{
 [Required]
 [StringLength(50,ErrorMessage = "Title cannot be longer than 50 characters")]
 public string Title { get; set; }
 
 [Required]
 public string Description { get; set; }

[Range(1, 10000,ErrorMessage = "The auction's starting price must be at least 1")]
 public decimal StartPrice { get; set; }
 
 public decimal CurrentPrice { get; set; }
 public DateTime EndTime { get; set; }
}

被渲染出的View的代码如下:

<h2>Create Auction</h2>
@using (Html.BeginForm())
{
 @Html.ValidationSummary()
 <p>
  @Html.LabelFor(model => model.Title)
  @Html.EditorFor(model => model.Title)
  @Html.ValidationMessageFor(model => model.Title, "*")
 </p>
 <p>
  @Html.LabelFor(model => model.Description)
  @Html.EditorFor(model => model.Description)
  @Html.ValidationMessageFor(model => model.Description, "*")
 </p>
 <p>
  @Html.LabelFor(model => model.StartPrice)
  @Html.EditorFor(model => model.StartPrice)
  @Html.ValidationMessageFor(model => model.StartPrice)
 </p>
 <p>
  @Html.LabelFor(model => model.EndTime)
  @Html.EditorFor(model => model.EndTime)
  @Html.ValidationMessageFor(model => model.EndTime)
 </p>
 <p>
 <input type="submit" value="Create" />
 </p>
}

被渲染出的HTML的代码如下:

<form action="/Auctions/Create" method="post" novalidate="novalidate">
 <div class="validation-summary-errors" data-valmsg-summary="true">
  <ul>
   <li>The Description field is required.</li>
   <li>The Title field is required.</li>
   <li>Auction may not start in the past</li>
  </ul>
 </div>
 <p>
  <label for="Title">Title</label>
  <input class="input-validation-error"
    data-val="true"
    data-val-length="Title cannot be longer than 50 characters"
    data-val-length-max="50"
    data-val-required="The Title field is required."
    id="Title" name="Title" type="text" value="">
  <span class="field-validation-error"
    data-valmsg-for="Title"
    data-valmsg-replace="false">*</span>
  </p>
  <p>
   <label for="Description">Description</label>
   <input class="input-validation-error"
    data-val="true"
    data-val-required="The Description field is required."
    id="Description" name="Description" type="text" value="">
   <span class="field-validation-error"
    data-valmsg-for="Description"
    data-valmsg-replace="false">*</span>
  </p>
  <p>
   <label for="StartPrice">StartPrice</label>
   <input data-val="true"
    data-val-number="The field StartPrice must be a number."
    data-val-range="The auction's starting price must be at least 1"
    data-val-range-max="10000"
    data-val-range-min="1"
    data-val-required="The StartPrice field is required."
    id="StartPrice" name="StartPrice" type="text" value="0">
   <span class="field-validation-valid"
    data-valmsg-for="StartPrice"
    data-valmsg-replace="true"></span>
  </p>
  <p>
   <label for="EndTime">EndTime</label>
   <input data-val="true"
    data-val-date="The field EndTime must be a date."
    id="EndTime" name="EndTime" type="text" value="">
   <span class="field-validation-valid"
    data-valmsg-for="EndTime"
    data-valmsg-replace="true"></span>
  </p>
  <p>
   <input type="submit" value="Create">
  </p>
</form>

我们看见在最后生成的HTML代码中多出了很多属性,这时候我们引入两个js文件:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript"</script>

这两文件会中的代码会根据那些多出的这些属性知道:
哪些input是需要验证的
验证的规则是什么
错误信息是什么
出现错误时应该怎样处理
我们称这个验证方式为隐式验证。

MVC客户端验证的小示例的更多相关文章

  1. MVC客户端验证配置

    <appSettings> <add key="ClientValidationEnabled" value="true"/> < ...

  2. asp.net mvc 客户端验证

    插件 jQuery unobtrusive Validation @Html.TextBoxFor(x=>x.UserName) [StringLength(7,MinimumLength=2, ...

  3. ASP.NET MVC 客户端验证失败后表单仍然提交问题

    客户端验证失败后表单仍然提交问题!导致页面刷新,辛辛苦苦输入的内容荡然无存. 多么奇怪的问题.按道理,验证失败,就应该显示各种错误信息,不会提交表单才对.而现在,错误信息正常显示,但页面却刷新了一遍. ...

  4. mvc 客户端验证

    @model MvcApplication1.Models.ViewClass @{ ViewBag.Title = "View2"; } @******引用这两个js实现客户端的 ...

  5. MVC客户端验证

    引用JS 注意:删除Layout里面默认引用的JQUERY,否则可能引起JS冲突. <link href="~/Content/Site.css" rel="sty ...

  6. asp.net mvc 模型验证组件——FluentValidation

    asp.net mvc 模型验证组件——FluentValidation 示例 using FluentValidation; public class CustomerValidator: Abst ...

  7. Identity Server 4 - Hybrid Flow - MVC客户端身份验证

    预备知识 可能需要看一点点预备知识 OAuth 2.0 不完全简介: https://www.cnblogs.com/cgzl/p/9221488.html OpenID Connect 不完全简介: ...

  8. 小修改,让mvc的验证锦上添点花(1)

    首先,mvc的客户端验证用的是jquery.validate.js, jquery.validate本身已经提供了很好的扩展功能,通过简单点配置就可以做得更好看些. 而Microsoft通过jquer ...

  9. 小修改,让mvc的验证锦上添点花

    首先,mvc的客户端验证用的是jquery.validate.js, jquery.validate本身已经提供了很好的扩展功能,通过简单点配置就可以做得更好看些. 而Microsoft通过jquer ...

随机推荐

  1. 写一个兼容性比较好的拖拽DEMO

    写一个兼容性比较好的拖拽DEMO 查看Demo 思路 div盒子 鼠标按下事件onmousedown 鼠标移动事件onmousemove,获得鼠标的坐标,将div移动至鼠标的当前坐标 鼠标抬起事件om ...

  2. nfs:server is not responding,still trying 原因与解决

    方案(学自他人) nfs:server is not responding,still trying的解决方法 (2009-04-20 10:20) 方法1 : 我在arm上通过NFS共享文件时出现下 ...

  3. C++ ofstream和ifstream详细用法

    转载地址:http://soft.chinabyte.com/database/460/11433960.shtml ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就 ...

  4. jqGrid一些操作

    formatter:function(cellvalue,options,rowObject){} 在格式化行的时候这三个参数 cellvalue行数, options配置信息, rowObject行 ...

  5. Hadoop Hive sql语法详解

    Hadoop Hive sql语法详解 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件 ...

  6. Robot Framework学习路线

    0. 官方网站  http://robotframework.org/ 所有资料都来自这里,从这里找到必要的链接,从而深入其中的细节. 1.  Quick Start Guide https://co ...

  7. uva - The Lottery(容斥,好题)

    10325 - The Lottery The Sports Association of Bangladesh is in great problem with their latest lotte ...

  8. 最全的LBS手机定位技术说明

    随着手机技术的发展定位方式也发生了非常大的变化.获取手机位置有非常多种方式. 第一种:CELL-ID定位原理 通过移动网络获取设备当前所在的Cell信息来获取设备当前位置.当设备位置更新设备会向当前服 ...

  9. Android之TextView------LINK的点击事件

    package com.TextHtml; import android.app.Activity; import android.content.Context; import android.os ...

  10. A - Number Sequence(矩阵快速幂或者找周期)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...