asp.net MVC2 利用 Model 模块设定进行数据验证

本节主要介绍ASP.Net MVC DropDownList绑定,以及自定义验证。首先看自定义验证,Model代码如下(需要引入System.ComponentModel.DataAnnotations)
using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;

public class SS_CODE
{    
         private string _C_CNAME;
        [Required(ErrorMessage = "不能为空" )]
        [StringLength(20, ErrorMessage = ( "不能超过20" ))]
         public      string C_CNAME
        {
              get {     return _C_CNAME; }
              set {    _C_CNAME = value; }
        }
}
我们加入两个验证,一个是非空,一个是验证字符串长度。
MVC下有下面几种验证。
Required :不能为空,
StringLength :字符 串的长度,
Range :数字的可输入范围,
RegularExpression :正则表达式匹配,
基本和传统的WebForms提供的验证一样。我们看看 页面代码
 
< %@ Page Title ="" Language=" C#" MasterPageFile ="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Model.SS_CODE > " %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="TitleContent" runat ="server" >
        400电话公告栏: http://www.my400800.cn
</ asp:Content >
< asp:Content ID ="Content2" ContentPlaceHolderID ="MainContent" runat ="server" >
         < h2 >
                400电话 公告栏</ h2 >
        <% using (Html.BeginForm())
                {%>
         < %: Html.TextBoxFor(model = > model.C_CNAME)%>
         < %: Html.ValidationMessageFor(model = > model.C_CNAME)%>
         < p >
                 < input type ="submit" value ="Create" />
         </ p >
         < %} % >
</ asp:Content >
< asp:Content ID ="Content3" ContentPlaceHolderID ="HeadContent" runat ="server" >
</ asp:Content >
运行一下,效果如图



asp.net MVC2 利用 Model 模块设定进行数据验证
 

 
 
 

接着我们看DropDownList的绑定。先看看控制器代码
public ActionResult Index()
                {
                         try
                        {
                                 object obj = base .GetClass( "CodeDao" );
                                List<SS_CODE> items = ((SS_CodeDao)obj).Select( " from SS_CODE" , 5);
                                IEnumerable<SS_CODE> ie = items.AsEnumerable();
                                SelectList list = new SelectList(ie, "C_DATA" , "C_ENAME" );
                                ViewData.Add( "items" , list);
                                 return View( "~/Views/System/Code/Index.aspx" );
                        }
                         catch (Exception e)
                        {
                                ILog lg = log4net.LogManager.GetLogger( "WebLogger" );
                                {
                                         if (lg.IsErrorEnabled)
                                        {
                                                lg.Error( this .GetType().ToString() + e.Message);
                                        }
                                }
                                 return View();
                        }
                }
再看看视图
<div>
                <%: Html.DropDownList( "ddlCName" , ViewData[ "items" ] as SelectList, "----选择----" )%>
        </div>
 

asp.net MVC2 利用 Model 模块设定进行数据验证
 
就这么简单,以后我们会讲到下拉列表三级联动。若要实现客户端验证,则需要引入 MicrosoftAjax.js和MicrosoftMvcValidation.js。并在using(Html.BeginForm())前加上Html.EnableClientValidation()即可 

你可能感兴趣的:(.net,mvc,IE,asp.net,asp)