介绍
这篇文章给大家分享的是有关ASP。净MVC中如何实现数据验证的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
首先我们在M层创建一个类:
using 系统; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using 来; using 包含; namespace WebApplication1.Models { ,,,public class GuestResponse ,,,{ ,,,,,,,//数据验证,必填项,如果不填ErrorMessage 请输入你的用户名 ,,,,,,,(要求(=ErrorMessage “请输入你的用户名!“)) ,,,,,,,public string Name {组,得到,,,} ,,,,,,,//同上 ,,,,,,,(要求(=ErrorMessage “请输入邮箱“)) ,,,,,,,//正则表达式,判断是否是邮箱格式 ,,,,,,,[RegularExpression (“+ \ \ @。+ \ \ . . +“, ,,,,,,,ErrorMessage =,“请输入正确的邮箱格式“) ,,,,,,,public string Email {组,得到,,,} ,,,,,,,//同上 ,,,,,,,(要求(=ErrorMessage “请输入你的手机号码“)) ,,,,,,,public string Phone {组,得到,,,} ,,,,,,,public bool ?, WillAttend {组,得到,,,} ,,,} }
代码中已有注释,不多说。
下面,V层:
@model WebApplication1.Models.GuestResponse @ { ,,,Layout =,空; } & lt; ! DOCTYPE html> & lt; html> & lt; head> ,,,& lt; meta name=皏iewport",内容=?device-width"宽度,/比; ,,,& lt; title> RsvpForm & lt;/head> & lt; body> ,,,@using (Html.BeginForm ()) ,,,{ ,,,,,,,@Html.ValidationSummary () ,,,,,,,& lt; p> Your 名称:,@Html.TextBoxFor (x =祝辞,x.Name), & lt;/p> ,,,,,,,& lt; p> Your 电子邮件:,@Html.TextBoxFor (x =祝辞,x.Email) & lt;/p> ,,,,,,,& lt; p> Your 电话:,@Html.TextBoxFor (x =祝辞,x.Phone) & lt;/p> ,,,,,,,& lt; p> ,,,,,,,,,,,Will 你参加吗? ,,,,,,,,,,,@Html.DropDownListFor (x =祝辞,x.WillAttend,,新的[],{ ,,,,,,,,,,,new SelectListItem (), {Text =,,是的,,我# 39;ll be there", ,,,,,,,,,,,Value =, bool.TrueString}, ,,,,,,,,,,,new SelectListItem (), {Text =,,不,,小姐:停下来# 39;t come", ,,,,,,,,,,,Value =, bool.FalseString} ,,,,,,,,,,,},,“Choose an option") ,,,,,,,& lt;/p> ,,,,,,,& lt; input 类型=皊ubmit",价值=https://www.yisu.com/zixun/"提交回复"/> } 身体>