文本框的范围验证在MVC3中不起作用 [英] Range validation for textbox not working in MVC3

查看:41
本文介绍了文本框的范围验证在MVC3中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.Net MVC 3应用程序中引发验证错误. 当用户输入的数字大于1000时,将显示错误消息.在视图模型上使用以下代码,它似乎不起作用.
我需要更改什么?

I am trying to raise validation error in an ASP.Net MVC 3 application. When the user enters a number that is greater than 1000 an error message should be displayed. with the following code on a view model it doesn't seem to be working.
What do i need to change?

[Range(0, 1000, ErrorMessage = "Total number of rows to display must be between 0 to 1000")]
 public int DisplayTop { get; set; }

cshtml:

@model  Calc.Models.CountingVM
@{
    ViewBag.Title = "Reports";
    Layout = "~/Views/Shared/_reportsLayout.cshtml";
}
@using (Html.BeginForm("Reports", "Calc", FormMethod.Post, new { @id = "frmReport" }))
{   
 .........
  @Html.TextBoxFor(c => c.DisplayTop, "1000")
  @Html.ValidationMessageFor(c => c.DisplayTop)
}

操作方法:

       public ActionResult Reports(string ReportName, CalcCriteria criteria)
            {

                  if ((criteria == null) || (criteria.nId == null))
                    {
                        criteria = TempData["criteria"] as CalcCriteria;
                        TempData["criteria"] = criteria; // For reload, without this reloading the page causes null criteria.
                    }
                  ReportType c = (ReportType)Enum.Parse(typeof(ReportType), ReportName, true);
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    string vmJson = string.Empty;

                    switch (c)
                    {
                           .....
                            int displayTop;
                           ..........
                            case ReportType.Inventory_Counts_Report:                      
                           ..............
                            displayTop = Convert.ToInt32(Request["DisplayTop"]);
                           ........
                     return View("Counting", CountingVM);
                     default:
                            return View();
                     }
                return View(); }

谢谢

BB

推荐答案

您还需要显示验证消息:

You also need to display the validation message:

@Html.ValidationMessageFor(c => c.DisplayTop)

这篇关于文本框的范围验证在MVC3中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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