asp.net mvc 3 数据类型验证

发布于 2024-11-08 21:36:29 字数 113 浏览 0 评论 0原文

我正在尝试实现数据类型的验证。我已经使用了 DataAnnotations,但对于数据类型,它没有显示自定义消息,

例如当我尝试将字符串数据输入 int 类型字段时。在这种情况下我如何自定义消息?

I am trying to realize valition on data type. I have used DataAnnotations, but for data type it's not showing customized message

for example when I' am trying enter string data into int typed field. How I can customize messages in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-11-15 21:36:29

如果我不得不猜测,您听起来像是希望在验证模型中的一个或多个字段时显示一条自定义消息。您可以子类化 DataAnnotations.ValidationAttribute 类并重写 IsValid(object) 方法,最后设置自定义 ErrorMessage 值(其中 ErrorMessage 已经属于 ValidationAttribute 类)

public class SuperDuperValidator : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        bool valid = false;
        // do your validation logic here
        return valid;
    }
}

最后,用属性装饰模型属性

public class MyClass
{
    [SuperDuperValidator(ErrorMessage="Something is wrong with MyInt")]
    public int MyInt { get; set; }
}

如果您使用的是开箱即​​用的 MVC3,这应该是正确验证所需的全部内容模型(尽管您的模型可能会有所不同/具有更多属性等)因此,在您的 [HttpPost] 控制器操作中,MVC 将自动绑定 MyClass 并且您将能够使用 ModelState.IsValid 确定发布的数据实际上是否有效。

If I had to guess, you sound like you want a custom message to display when validating one or more fields in your model. You can subclass the DataAnnotations.ValidationAttribute class and override the IsValid(object) method and finally setting a custom ErrorMessage value (where ErrorMessage already belongs to the ValidationAttribute class)

public class SuperDuperValidator : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        bool valid = false;
        // do your validation logic here
        return valid;
    }
}

Finally, decorate your model property with the attribute

public class MyClass
{
    [SuperDuperValidator(ErrorMessage="Something is wrong with MyInt")]
    public int MyInt { get; set; }
}

If you're using out-of-the-box MVC3, this should be all you need to propertly validate a model (though your model will probably differ/have more properties, etc) So, in your [HttpPost] controller action, MVC will automagically bind MyClass and you will be able to use ModelState.IsValid to determine whether or not the posted data is, in fact, valid.

逆蝶 2024-11-15 21:36:29

Pavel,

DataAnnotations DataType 属性不会影响验证。它用于决定如何呈现您的输入。在这种情况下,大卫的上述解决方案有效。

但是,如果您只想使用内置验证属性,则可能需要像这样使用 Range 属性:(

[Range(0, 10, ErrorMessage="Please enter a number between 0 and 10")]
public int MyInt { get ; set ;}

当然,您确实应该使用 ErrorMessageResourceName/Type 参数并将硬编码的错误消息字符串提取到resx 文件。)

确保让 MVC 知道在哪里呈现错误消息:

<%= Html.ValidationMessageFor(m => m.MyInt) %>

或者您可以只使用 EditorForModel,它会正确设置它。

Pavel,

The DataAnnotations DataType attribute does not affect validation. It's used to decide how your input is rendered. In such a case, David's solution above works.

However, if you want to use only the built-in validation attributes, you probably need to use the Range attribute like this:

[Range(0, 10, ErrorMessage="Please enter a number between 0 and 10")]
public int MyInt { get ; set ;}

(Of course, you should really be using the ErrorMessageResourceName/Type parameters and extract out hard-coded error message strings into resx files.)

Make sure to let MVC know where to render your error message:

<%= Html.ValidationMessageFor(m => m.MyInt) %>

Or you can just use EditorForModel and it will set it up correctly.

我要还你自由 2024-11-15 21:36:29

我认为这个问题还没有得到解答,因为我也有同样的问题。

如果您的模型具有 int 类型的属性,并且用户输入字符串“asd”,则 MVC3 框架绑定/验证将介入并导致您的视图显示 “值 'asd' 对于以下内容无效<此处为模型属性名称或 DisplayName>"

对我来说,发帖者问 MVC3 框架输出的这条消息可以定制吗?
我也想知道。虽然该消息还不错,但如果您将字段标记为可以轻松指示需要数字的内容,您可能仍希望包含其他原因,因此它会显示如下内容:

“值 'asd' 对于 < 无效;fieldname>; 必须是正整数。”

这样用户就不会输入一个又一个的值,并且每次都会收到不同的错误消息。

I don't think this has been answered because I have the same issue.

If you have a Model with a property of type int and the user types in a string of "asd" then the MVC3 framework binding/validation steps in and results in your view displaying "The value 'asd' is not valid for <model property name or DisplayName here>".

To me the poster is asking can this message that the MVC3 framework is outputting be customized?
I'd like to know too. Whilst the message is not too bad if you label your field something that easily indicates an number is expected you might still want to include additional reasons so it says something like:

"The value 'asd' is not valid for <fieldname>; must be a positive whole number."

So that the user is not entering value after value and getting different error messages each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文