MVC中的自定义验证(剃刀) [英] Custom validation In MVC (Razor)

查看:110
本文介绍了MVC中的自定义验证(剃刀)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有两个属性(FirstName,LastName),它们与视图绑定.并且两者都是必需的.但是,我们只需要一条错误消息.

赞(姓氏和名字必填")
因此,请给我一些想法进行此操作.


谢谢

Hello Everybody,


I have Two Properties (FirstName ,LastName) that i am binding with a view. And Both are required. But there We want only one Error Message.

Like ("first Name and Last name Required")
So please give me some idea to do this operation.


Thanks

推荐答案

,您可以使用内置的DataAnnotations

例如:

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Globalization;
使用System.Web.Mvc;
使用System.Web.Security;

命名空间MvcApplication1.Models
{

公共类ChangePasswordModel
{
[必填]
[DataType(DataType.Password)]
[显示(名称=当前密码")]
公共字符串OldPassword {get;放; }

[必填]
[StringLength(100,ErrorMessage ="{0}的长度至少应为{2}个字符.",MinimumLength = 6)]
[DataType(DataType.Password)]
[显示(名称=新密码")]
公共字符串NewPassword {get;放; }

[DataType(DataType.Password)]
[显示(名称=确认新密码")]
[Compare("NewPassword",ErrorMessage =新密码和确认密码不匹配.")]
公共字符串ConfirmPassword {get;放; }
}
}

------或-------

您可以阅读本文以使用javascript/Jquery进行自定义验证

http://stevenalexander.posterous.com/aspmvc-custom-validation-without-using-data-a#!/
you can use the built-in DataAnnotations

example:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication1.Models
{

public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}

------ Or -------

you can read this article for custom validation using javascript/Jquery

http://stevenalexander.posterous.com/aspmvc-custom-validation-without-using-data-a#!/


这篇关于MVC中的自定义验证(剃刀)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆