Let's say I created a model with a property as int?
type.
public class MyModel
{
[Range(-100, 100, ErrorMessage="Too large")]
public int? MyValue { get; set; }
}
In the view I use it for a text box in the form to post back to server.
@using (Html.BeginForm())
{
<p>Enter some value</p>
<div>@Html.TextBoxFor(m => m.MyValue)</div>
<div>@Html.ValidationMessageFor(m => m.MyValue)</div>
<input type="submit" value="Submit" />
}
Here is the controller code
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
return View(model);
}
}
Now if I enter an extremely large number in the text box, e.g 111111111111111111111111111111111111111111111111111111111111111111111111111111
, which is for sure too large for an integer data type. On post back, got message The value '111111111111111111111111111111111111111111111111111111111111111111111111111111' is invalid.
But I have no way to use the error message (localization and so on) I defined in my model.