验证在int数据类型的ASP.NET MVC3 [英] Validation on int data type asp .net mvc3

查看:136
本文介绍了验证在int数据类型的ASP.NET MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到值'ABC'是无效的字段名。的错误消息。这是默认的错误消息,我想重写它更简单的方法。
截至目前我已经尝试下面

I am getting The value 'abc' is not valid for fieldName. as error message. which is default error message and i want to override it in easier way.
as of now what i have tried is listed below


  • [RegularEx pression(@^ \\ D + $的ErrorMessage =请输入数值)]

  • [整数(ErrorMessageResourceType = typeof运算(appName.Resources.abc.Resource)
                   ErrorMessageResourceName =error_numeric)]

  • [RegularEx pression(([1-9] [0-9] *))]

  • 范围(1,int.max,的ErrorMessage =请输入数值)

    但未能改变默认的错误消息。


    我建议的最简单的方式来做到这一点。

  • [RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")]
  • [Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource), ErrorMessageResourceName = "error_numeric")]
  • [RegularExpression("([1-9][0-9]*)")]
  • Range(1,int.max,ErrorMessage="enter numeric value")
    but failed to change default error message.
    Suggest me the easiest possible way to do this.

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.ComponentModel.DataAnnotations; 
   using System.Web.Mvc;

  namespace blueddPES.ViewModels
     {
     public class ContactViewModel
        {
         [Integer(ErrorMessage="sdfdsf")]
         public int? hp { get; set; }
        }


推荐答案

最简单的方法是使用数据注释扩展。它有一些有用的属性的东西像整数,等等。

Easiest way is to use Data Annotations Extensions. It has some useful attributes for things like Integers, etc.

或者你可以写自己的,像:如何更改数据-VAL-号码消息验证MVC中,而它产生由助手

Or you could write your own, like in: How to change 'data-val-number' message validation in MVC while it generate by helper

修改:意见后添加完整的示例

Edit: Added complete sample after comments.

我创建了一个示例香草MVC 3项目,然后做了以下内容:

I created a sample vanilla MVC 3 project and then did the following:


  1. 新增的NuGet包 DataAnnotationsExtensions.MVC3

添加了一个模型类:

public class IntegerSample
{
    [Required(ErrorMessage="Dude, please fill something in!")]
    [Integer(ErrorMessage="Are you stupid? Just fill in numbers only!")]
    public int? TestValue { get; set; }
}


  • 增加了一个家庭控制器:

  • Added a Home Controller:

    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
    }
    


  • 增加了一个景观:

  • Added a Home View:

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>IntegerSample</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.TestValue)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.TestValue)
                @Html.ValidationMessageFor(model => model.TestValue)
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }
    


  • 我希望你能使用此示例code一些见解。当我运行这个样本,它的工作原理就像你所想期待。

    I hope you get some more insights using this sample code. When I run this sample, it works just as you'd want to expect.

    这篇关于验证在int数据类型的ASP.NET MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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