MVC3 删除 ModelState 错误
我遇到了一种情况,我正在上传用户从本地文件系统中选择的图像。在我看来,我的表单基本上有两个提交按钮。一种用于正常提交表单,并执行所有验证。第二个仅用于上传图像,在这种情况下我还不想验证。
我设法通过为我的“上传图像”提交按钮提供一个类值“style-name cancel”来关闭客户端验证,所以
<input type="submit" name="UploadImageButton" value="Upload Image" class="style-name cancel" />
现在,当我回发时,我的模型有一个属性 UploadImageButton,当单击此按钮时,它填充此属性(输入的名称与属性匹配)。这样,我就知道表单是通过真正的“提交”按钮还是通过“UploadImageButton”提交的。
我的问题是这个... 如何关闭服务器端验证?我不希望用户单击此按钮时显示验证摘要信息。我知道您可以使用此添加自定义模型错误,
ModelState.AddModelError("{key}", "{error msg}");
我正在寻找一种删除模型错误的方法。这可能吗?
编辑:
这是我想出的:
foreach (var key in ModelState.Keys.ToList().Where(key => ModelState.ContainsKey(key))) {
//ModelState.Remove(key); //This was my solution before
ModelState[key].Errors.Clear(); //This is my new solution. Thanks bbak
}
I've got a situation where I'm uploading an image the user has selected from his local file system. My Form in my view, basically has two submit buttons. One is used to Submit the form normally, and all validation executes. The 2nd is only for uploading the image, in which case I don't want to validate yet.
I managed to turn off Client Side validation by giving my 'Upload Image' submit button an a class value of "style-name cancel" , so
<input type="submit" name="UploadImageButton" value="Upload Image" class="style-name cancel" />
Now, when I post back, my model has a property UploadImageButton, when this button is clicked, it populates this property (Name of the input matches the Property). This way, I know whether the form was submitted by my true Submit button or by the UploadImageButton.
My question is this...
How can I turn off ServerSide validation? I don't want the Validation Summary info showing up when the user clicks this button. I know you can add custom model errors using this
ModelState.AddModelError("{key}", "{error msg}");
I'm looking for a means to Remove Model Errors. Is this possible?
EDIT:
Here is what I came up with:
foreach (var key in ModelState.Keys.ToList().Where(key => ModelState.ContainsKey(key))) {
//ModelState.Remove(key); //This was my solution before
ModelState[key].Errors.Clear(); //This is my new solution. Thanks bbak
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过执行以下操作来消除模型错误:
You can remove model errors by doing something like this:
这建立在以前的答案的基础上,但进一步简化了一些:
This builds off previous answers, but simplifies it a little more:
我有时会用到它,所以我用它做了一个扩展方法:
可以流畅地使用它来清除多个字段的错误。
I use it sometimes, so I've made an extension method out of it:
It can be used fluently to clear error for multiple fields.
一般来说,您不想关闭服务器端验证或手动删除 ModelState 错误。这是可行的,但容易出错,因为它必须依赖字符串作为键。我对如何删除钥匙的答案投了赞成票,因为它是正确且有用的,但我建议不要将自己设计成必须强烈考虑的情况。
在您的情况下,同一个表单有多个提交按钮,但在上传图像时您实际上并没有提交表单。为了防止客户端验证,您可以使用“取消”类,正如您已经指出的那样,但我还建议使用不同的表单中的第二个提交按钮,在这种情况下,它会导致主表单上没有验证。
使用不同形式还有第二个优点:您可以将其发送到不同的 ActionResult 方法,该方法仅处理上传图像的逻辑。这种不同的方法根本不会费心检查模型状态的“IsValid”属性:它只关心图像中是否有信息,并且可以单独检查。 (即使您使用相同的 ActionResult 方法,您也可以集中逻辑,以便不依赖于模型状态的 IsValid 属性。)
如果必须清除模型状态中的错误,那么清除所有错误是有意义的其中,尝试以下操作:
这使您不必依赖于正确获取键名称,但如果模型中已存在任何值(有效或无效),则保留这些值。
在这些情况下要检查的最后一件事是您的值是否仅有时出现在视图中,这不会导致客户端验证(它们不在视图中),但会导致服务器端验证问题。在这种情况下,最好使用@Html.HiddenFor(model => model.PropertyName,如果可以的话,假设该值已经设置,它在此视图中不可见。
In general, you don't want to turn off the server-side validation or remove ModelState errors manually. It's doable, but error-prone since it must rely on strings as keys. I upvoted the answer on how to remove the keys, because it is correct and useful, but I recommend against designing yourself into a case where you must strongly consider it.
In your case, you have multiple submit buttons for the same form, but you really aren't submitting the form when you upload the image. To prevent the client-side validation, you can use the "cancel" class as you've already indicated, but I also recommend having the 2nd submit button in a different form, in which case it causes no validation on your main form.
There is a second advantage to using the different form: you can send it to a different ActionResult method which would just handle the logic of uploading the image. This different method would simply not bother to check the "IsValid" property of the model state: it only cares if there is information in the image, and that can be checked separately. (Even if you use the same ActionResult method, you can funnel the logic so that you don't rely on the IsValid property of the model state.)
If you must clear the errors from the model state, and it makes sense to clear all of them, try this:
This keeps you from depending on getting the key names correct, but preserves the values should any values (valid or invalid) already exist in the model.
One last thing to check for in these cases is whether you have values that are only sometimes in the view, which will not cause client-side validation (they're not in the view), but do result in server-side validation issues. In this case, it's best to use @Html.HiddenFor(model => model.PropertyName, if you can, assuming the value has already been set, it just isn't visible in this view.
使用 ModelState.Remove("{key}") 从 model 中删除错误,这会将 ModelState.IsValid 重置为 true
use ModelState.Remove("{key}") to remove the error from model , which will reset the ModelState.IsValid to true
下面的代码循环遍历所有错误并删除不在所需页面中的错误。我有两页,我只想显示第一页的错误。 (第二页已隐藏,但稍后当第一页隐藏时将可见。)您只需获取错误并循环并删除当前可见页面中不存在的错误即可。 PageNum是之前设置的整数。
Here's code that loops through all the errors and removes the ones that are NOT in the desired page. I have two pages and I only want to show the errors for the 1st page. (The 2nd page is hidden but will be visible later when page 1 is hidden.) You simply get the errors and loop through and remove the errors that are not in the currently visible page. PageNum is an integer set earlier.