我想在ASP.Net MVC 3应用程序中引发验证错误。 当用户输入一个大于1000的数字时,应显示错误消息。在视图模型上使用下面的代码,它似乎不工作。
我需要改变什么?范围验证的文本框不工作在MVC3
[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
您的cshtml或aspx视图是什么样的?你的控制器动作是什么样的? – Andorbal 2013-02-15 20:26:12
您可能未检查控制器中的ModelState ... – 2013-02-15 20:27:28