ASP.NET MVC 2自定义验证
2011-02-17 00:34
471 查看
我将创建一个PriceAttribute来验证某个值是否大于指定的价格,并且这个价格必须以99分结束,因此$20.00是无效的值,$19.99是有效的。下面是这个属性的代码:
以下是代码片段:
public class PriceAttribute : ValidationAttribute {
public double MinPrice { get; set; }
public override bool IsValid(object value) {
if (value == null) {
return true;
}
var price = (double)value;
if (price < MinPrice) {
return false;
}
double cents = price - Math.Truncate(price);
if(cents < 0.99 || cents >= 0.995) {
return false;
}
return true;
}
}
注意如果值为空,返回的值是true,这个属性不会验证字段是否需要。我会在RequiredAttribute中验证值是否需要。它允许我将属性放在可选的值上,当用户将这个字段留为空时显示一个错误。
我们可以创建一个视图模型,然后应用这个属性到模型上进行快速测试,下面是这个模型的代码:
以下是代码片段:
public class ProductViewModel {
[Price(MinPrice = 1.99)]
public double Price { get; set; }
[Required] www.szfuao.com
public string Title { get; set; }
}
我们再快速地创建一个视图(Index.aspx)显示和编辑窗体:<%@ Page Language="C#" Inherits="ViewPage" %>
以下是代码片段:
<%@ Page Language="C#" Inherits="ViewPage" %>
<% using (Html.BeginForm()) { %>
<%= Html.TextBoxFor(m => m.Title) %>
<%= Html.ValidationMessageFor(m => m.Title) %>
<%= Html.TextBoxFor(m => m.Price) %>
<%= Html.ValidationMessageFor(m => m.Price) %>
<input type="submit" />
<% } %> <% using (Html.BeginForm()) { %><%= Html.TextBoxFor(m => m.Title) %><%= Html.ValidationMessageFor(m => m.Title) %><%= Html.TextBoxFor(m => m.Price) %><%= Html.ValidationMessageFor(m => m.Price) %><% } %>
现在我们只需要一个有两个行为的控制器,一个编辑视图,另一个接收提交的ProductViewModel。
以下是代码片段:
[HandleError] www.yzjxsp.com
public class HomeController : Controller {
public ActionResult Index() {
return View(new ProductViewModel());
}
[HttpPost]
public ActionResult Index(ProductViewModel model) {
return View(model);
}
}
以下是代码片段:
public class PriceAttribute : ValidationAttribute {
public double MinPrice { get; set; }
public override bool IsValid(object value) {
if (value == null) {
return true;
}
var price = (double)value;
if (price < MinPrice) {
return false;
}
double cents = price - Math.Truncate(price);
if(cents < 0.99 || cents >= 0.995) {
return false;
}
return true;
}
}
注意如果值为空,返回的值是true,这个属性不会验证字段是否需要。我会在RequiredAttribute中验证值是否需要。它允许我将属性放在可选的值上,当用户将这个字段留为空时显示一个错误。
我们可以创建一个视图模型,然后应用这个属性到模型上进行快速测试,下面是这个模型的代码:
以下是代码片段:
public class ProductViewModel {
[Price(MinPrice = 1.99)]
public double Price { get; set; }
[Required] www.szfuao.com
public string Title { get; set; }
}
我们再快速地创建一个视图(Index.aspx)显示和编辑窗体:<%@ Page Language="C#" Inherits="ViewPage" %>
以下是代码片段:
<%@ Page Language="C#" Inherits="ViewPage" %>
<% using (Html.BeginForm()) { %>
<%= Html.TextBoxFor(m => m.Title) %>
<%= Html.ValidationMessageFor(m => m.Title) %>
<%= Html.TextBoxFor(m => m.Price) %>
<%= Html.ValidationMessageFor(m => m.Price) %>
<input type="submit" />
<% } %> <% using (Html.BeginForm()) { %><%= Html.TextBoxFor(m => m.Title) %><%= Html.ValidationMessageFor(m => m.Title) %><%= Html.TextBoxFor(m => m.Price) %><%= Html.ValidationMessageFor(m => m.Price) %><% } %>
现在我们只需要一个有两个行为的控制器,一个编辑视图,另一个接收提交的ProductViewModel。
以下是代码片段:
[HandleError] www.yzjxsp.com
public class HomeController : Controller {
public ActionResult Index() {
return View(new ProductViewModel());
}
[HttpPost]
public ActionResult Index(ProductViewModel model) {
return View(model);
}
}
相关文章推荐
- ASP.NET MVC自定义验证Authorize Attribute
- 在ASP.NET MVC中使用Knockout实践07,自定义验证信息的位置与内容
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- ASP.NET MVC验证 - 自定义验证规则、验证2个属性值不等【待验证】
- ASP.NET mvc 自定义验证和Filter过滤器传参
- ASP.NET MVC系列之 如何实现自定义验证(服务端验证+客户端验证)
- ASP.NET MVC ValidationAttribute 服务器端自定义验证
- ASP.Net MVC 自定义用户验证
- 在ASP.NET MVC中使用Knockout实践06,自定义验证、异步验证
- ASP.NET MVC unobtrusive客户端自定义验证
- ASP.NET MVC自定义验证的一个小坑
- 初识ASP.NET MVC窗体验证与权限过滤---3.自定义过滤器验证Session超时
- ASP.NET MVC 3 使用Model自定义验证的样式
- ASP.NET MVC ValidationAttribute 服务器端自定义验证
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
- MVC学习十四:ASP.NET MVC如何实现自定义验证 AgeRangeAttribute
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
- asp.net MVC中如何用Membership类和自定义的数据库进行登录验证