[翻译]ASP.NET MVC 3 开发的20个秘诀(三)[20 Recipes for Programming MVC 3]:验证用户输入...

本文详细介绍了如何在.NET4.0的MVC3框架中利用DataAnnotations命名空间提供的元数据属性进行表单验证,确保用户输入的图书信息与数据库和模型设计的一致性。通过引入自定义验证方法,实现对ISBN的特殊验证逻辑,确保数据准确性和完整性。此外,文章还展示了如何通过属性类定制价格、出版时间等字段的验证规则,以及如何通过验证状态判断表单输入的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

议题

在设计的时候要添加验证以保证表单中输入的内容与数据库和模型设计的类型相符。

解决方案

在.NET 4.0的MVC 3中包含新的命名空间DataAnnotations提供了许多有用的元数据属性。为了验证表单的输入,以下的属性类可以提供多种验证方式:RequireAttribute,RegularExpressionAttribute和DataTypeAttribute。当需要定义必须输入的内容时 ,MVC 3支持开发人员通过改进的ValidationAttribute类对验证进行定义。

 

讨论

在先前代码优先的秘诀中我们创建Book模型,我们将进行以下更新:

  1. 输入书名;
  2. 输入并验证ISBD;
  3. 输入书籍摘要;
  4. 输入书籍作者;
  5. 输入并验证书籍的价格(美元);
  6. 输入并验证出版时间。 

6个输入项中有5个的验证可以通过MVC 3的内置验证方法来定制。但是ISBN的格式需要不同的验证方式 --- 自定义验证方法:

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using MvcApplication4.Validations;

namespace MvcApplication4.Models
{
public class Book
{
public int ID { get; set; }

[Required]
public string Title { get; set; }

[Required]
[IsbnValidation]
public string Isbn { get; set; }

[Required]
public string Summary { get; set; }

[Required]
public string Author { get; set; }

public string Thumbnail { get; set; }

[Range(1, 100)]
public double Price { get; set; }

[DataType(DataType.Date)]
[Required]
public DateTime Published { get; set; }
}

public class BookDBContext : DbContext
{
public DbSet<Book> Books { get; set; }
}

}

 

在上面这个例子中,由[Require]属性来标识用户必须输入字段。必须要在IsbnValidationAttribute类中添加IsValid属性,以便在操作的时候通知MVC 3调用。为了验证价格,在Price字段上面添加[Range]类,并设置[RegularExpression]属性如下:

        [Range(1, 100)]
[RegularExpression(@"(\b[\d\.]*)")]
public double Price { get; set; }

 

最后,通过定义DataType属性告诉MVC,published成员类型是时间类型。当前IsbnValidation类并不会在验证时候显示任何错误,接下来我们将实现它。

ISBN的有效长度为10至13个字符长度。为了更好的组织代码,将自定义验证代码以及其他验证代码文件放置在单独的文件夹中保存。右键单击该项目,并选择“添加”->“新建文件夹”,并将该文件夹重命名为“Validations”。然后选择“添加”->“类”,将其命名为”IsbnValidationAttribute.cs”,这个类将继承ValidationAttribute类以及重写IsValid方法以验证输入的ISBN内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace MvcApplication4.Validations
{
[AttributeUsage(AttributeTargets.Field |
AttributeTargets.Property, AllowMultiple = false,
Inherited = true)]
public class IsbnValidationAttribute :
System.ComponentModel.DataAnnotations.ValidationAttribute
{
/**
* This class is courtesy:
*
http://www.java2s.com/Open-Source/CSharp/
* Inversion-of-Control-Dependency-Injection/Spring.net/
* Spring/Validation/Validators/ISBNValidator.cs.htm
*
* This class is used for demonstration purposes
* of performing an ISBN validation. Should you
* wish to use this in your project, please
* consult the license agreement here:
*
http://www.apache.org/licenses/LICENSE-2.0
*
*/

private static readonly String SEP = "(?:\\-|\\s)";
private static readonly String GROUP = "(\\d{1,5})";
private static readonly String PUBLISHER = "(\\d{1,7})";
private static readonly String TITLE = "(\\d{1,6})";



static readonly String ISBN10_PATTERN =
"^(?:(\\d{9}[0-9X])|(?:" + GROUP + SEP + PUBLISHER +
SEP + TITLE + SEP + "([0-9X])))$";

static readonly String ISBN13_PATTERN =
"^(978|979)(?:(\\d{10})|(?:" + SEP + GROUP + SEP +
PUBLISHER + SEP + TITLE + SEP + "([0-9])))$";

public IsbnValidationAttribute() :
base("Invalid ISBN number")
{

}

public override bool IsValid(object value)
{
// Convert to string and fix up the ISBN
string isbn = value.ToString();

string code = (isbn == null)
? null :
isbn.Trim().Replace("-", "").Replace(" ", "");

// check the length
if ((code == null) || (code.Length < 10
|| code.Length > 13))
{
return false;
}

// validate/reformat using regular expression
Match match;

String pattern;

if (code.Length == 10)
{
pattern = ISBN10_PATTERN;
}
else
{
pattern = ISBN13_PATTERN;
}

match = Regex.Match(code, pattern);

return match.Success && match.Index == 0 &&
match.Length == code.Length;
}
}
}

上面的这个例子是一个C#开源示例包含一个ISBN标准验证程序。IsValid方法会匹配两个验证规则,如果通过就会返回True,否则返回False并请求用户重新输入。 

 

当你访问并提交书籍创建页面的时,就会出现上述错误信息,直到所有输入项都输入了正确的数据。这个检测通过与否要判断ModelState.IsValid属性是否为True。

 

参考信息

DataAnnotations Namespace 原书地址 书籍源代码

转载于:https://www.cnblogs.com/o2ds/archive/2011/12/06/2278559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值