我已经为我的表单建立了模型验证,但是验证似乎根本不起作用。我想没人能帮上忙。我试过使用下面的工作方法,但在firebug中一直存在一个“未定义的”错误。
示例工作脚本:
<script type="text/javascript">
$(document).ready(function () {
$.validator.setDefaults({ ignore: [] });
});
</script>
示例文本字段(注:带有客户名称的自动完成文本字段):
@Html.TextBox("txtCustomer", null, new { @id = "txtCustomer", @class = "txt" })
示例隐藏字段(注意:当从自动完成字段进行选择时,id被注入隐藏字段):
@Html.HiddenFor(model => model.Customer_ID, new { @id = "hCustomerID" })
示例模型数据注释
[Range(1, Int32.MaxValue, ErrorMessage = "Please select a customer")]
public int Customer_ID { get; set; }
编辑:错误的屏幕截图
对不起,我不得不张贴到截图链接,因为我的代表点足够高。
UI回发错误
编辑:显示页面源的屏幕截图
网页源截图
发布于 2013-01-24 16:57:23
在不显眼的脚本之后,以这种方式更改验证器上的忽略已经太晚了。这是因为验证器只在创建时提取默认值一次。不显眼的脚本为您创建验证器。您需要引用现有的验证器对象并更新它。
尝尝这个
<script>
$(document).ready(function () {
$('form').validate().settings.ignore = []
});
</script>
发布于 2016-03-29 05:07:29
如果不必在Controller和相关视图的操作中使用javascript,则可以在验证模型之前添加模型错误。示例:
[HttpPost]
public ActionResult Fix(YourModel mdl)
{
if (mdl.Customer_ID>Int32.MaxValue || mdl.Customer_ID<1)
ModelState.AddModelError("", "Your error message!");
if (ModelState.IsValid)
{
//
//Some code
//
return View("YourView", yourlist);
}
return View(mdl);
}
https://stackoverflow.com/questions/14503698
复制相似问题