Questions tagged «asp.net-mvc-2-validation»

9
ASP.NET MVC Html.ValidationSummary(true)不显示模型错误
我对Html.ValidationSummary有一些问题。我不想在ValidationSummary中显示属性错误。当我设置Html.ValidationSummary(true)时,它不会显示来自ModelState的错误消息。当对字符串的控制器操作中出现某些异常时 MembersManager.RegisterMember(member); catch部分将错误添加到ModelState: ModelState.AddModelError("error", ex.Message); 但是ValidationSummary不会显示此错误消息。当我设置Html.ValidationSummary(false)时,将显示所有消息,但我不想显示属性错误。我该如何解决这个问题? 这是我正在使用的代码: 模型: public class Member { [Required(ErrorMessage = "*")] [DisplayName("Login:")] public string Login { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Password)] [DisplayName("Password:")] public string Password { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Password)] [DisplayName("Confirm Password:")] public string ConfirmPassword { get; set; } } 控制器: …

3
ModelState.AddModelError-如何添加非属性错误?
我正在检查数据库,Create(FooViewModel fvm){...}以查看fvm.prop1和fvm.prop2在该组合中是否已经存在;如果是这样,我想向模型状态添加错误,然后返回整个视图。我试过了: public ActionResult Create(FooViewModel fvm){ if (ThatComboAlreadyExists(fvm)) { ModelState.AddModelError("Model", "There is already one like that"); return View(fvm); } } ...但是我没有显示中的错误Html.ValidationSummary,我认为这是错误的出现位置。我怀疑“模型”不是正确的键,但是我找不到Google那样的东西。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.