在做表单验证的时候,我们一般都使用javascript或是jquery这种客户端的验证。比较好用,而且比较轻量级。在MVC中它也给我们提借了一种model验证,样式也很不错。在这里做一个简单的说明。
要想做model验证, 必须要在model层先进行处理,先要引入using System.ComponentModel.DataAnnotations命名空间。下面是我的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace TestMVC.Models
{
public class Eorgans
{
/// <summary>
/// 机构标识种子
/// </summary>
[Range(0, 9, ErrorMessage = "只能输入0-9之间的数")]
public int sortId { get; set; }
/// <summary>
/// 机构名称
/// </summary>
[Required(ErrorMessage = "机构名称不为空")]
public string or_Name { get; set; }
/// <summary>
/// 机构的sortcode
/// </summary>
[StringLength(4), Required(ErrorMessage = "字符最多4位")]
public string sortCode { get; set; }
/// <summary>
///
/// </summary>
public string or_id { get; set; }
}
}
这里面我给出了,三种常用的验证方法,一种是不为空,一种字符串长度,一种是数值区间。
下面我们来看一下C层和V层的代码。
C层,C层这里要说明一点,C层的添加表单和保存必须是一个名,这是一个很重要的一点,一开始,我用的是二个名,可是死活进行不了验证。代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMVC.Models;
namespace TestMVC.Controllers
{
public class OrganController : Controller
{
//
// GET: /Organ/
DBClass db = new DBClass();
public ActionResult Index()
{
IEnumerable<Eorgans> list = db.organsList();
return View(list);
}
public ActionResult AddOrgan()
{
Eorgans eo = new Eorgans();
return View(eo);
}
//这里就是我们要注意的地方,它的名字必须和上面表单页面的名字一样,不然不会运行
//这个httppost也很重要,它告诉程序,这个是用来接收的
[HttpPost]
public ActionResult AddOrgan(Eorgans eo)
{
if (ModelState.IsValid)
{
string tempstr = eo.or_Name + eo.sortCode + eo.sortId;
return Content(tempstr);
}
return View(eo);
}
}
}
V层就比较简单了,
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestMVC.Models.Eschool>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
add
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>add</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.number) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.number) %>
<%= Html.ValidationMessageFor(model => model.number) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.schoolCode) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.schoolCode) %>
<%= Html.ValidationMessageFor(model => model.schoolCode) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.schoolName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.schoolName) %>
<%= Html.ValidationMessageFor(model => model.schoolName) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.sortcode) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.sortcode) %>
<%= Html.ValidationMessageFor(model => model.sortcode) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>