MVC3 删除 ModelState 错误 [英] MVC3 Remove ModelState Errors

查看:30
本文介绍了MVC3 删除 ModelState 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一种情况,我正在上传用户从其本地文件系统中选择的图像.在我看来,我的表单基本上有两个提交按钮.一个用于正常提交表单,执行所有验证.第二个仅用于上传图像,在这种情况下我还不想验证.

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" /> 

现在,当我回发时,我的模型有一个属性 UploadImageButton,当这个按钮被点击时,它会填充这个属性(输入的名称与属性匹配).这样,我就知道表单是由我真正的提交按钮提交的还是由 UploadImageButton 提交的.

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?

这是我想出的:

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
}

推荐答案

您可以通过执行以下操作来消除模型错误:

You can remove model errors by doing something like this:

if (ModelState.ContainsKey("{key}"))
    ModelState["{key}"].Errors.Clear();

这篇关于MVC3 删除 ModelState 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆