简体   繁体   English

如何在ASP.NET MVC 3中的HttpPost操作中禁用验证?

[英]How to disable validation in a HttpPost action in ASP.NET MVC 3?

I have a Create-View like this ... 我有这样的创建视图......

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
        type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
        type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(null, new { @class = "validation" })
    ...
    <input class="cancel" type="submit" value="OK" />
    ...
    <input name="submit" type="submit" value="Save" />
}

... and a corresponding controller action: ......以及相应的控制器动作:

[HttpPost]
public ActionResult Create(string submit, MyViewModel myViewModel)
{
    if (submit != null) // true, if "Save" button has been clicked
    {
        if (ModelState.IsValid)
        {
            // save model data
            return RedirectToAction("Index");
        }
    }
    else // if "OK" button has been clicked
    {
        // Disable somehow validation here so that
        // no validation errors are displayed in ValidationSummary
    }

    // prepare some data in myViewModel ...

    return View(myViewModel); // ... and display page again
}

I have found that I can disable client-side validation by setting class="cancel" on the "OK" button. 我发现我可以通过在“确定”按钮上设置class="cancel"来禁用客户端验证。 This works fine. 这很好用。

However, server-side validation still happens. 但是,服务器端验证仍然会发生。 Is there a way to disable it in a controller action (see the else-block in the Create action above)? 有没有办法在控制器操作中禁用它(请参阅上面的Create操作中的else-block)?

Thank you for help! 谢谢你的帮助!

I recently had a similar problem. 我最近有类似的问题。 I wanted to exclude some properties from validation and used the following code: 我想从验证中排除一些属性并使用以下代码:

ModelState.Remove("Propertyname");

To hide the errormessages you can use 隐藏可以使用的错误消息

ModelState.Clear();

But the question is why you submit the values if you do not use them? 但问题是,如果您不使用这些值,为什么要提交这些值? Would you not better use a reset button to reset the values in the form: 您是否最好使用重置按钮重置表单中的值:

<input type="reset" value="Reset Form">

So if there is nothing in your submit string you want it to ignore checking if the model state is valid and assume that it is. 因此,如果您的提交字符串中没有任何内容,您希望它忽略检查模型状态是否有效并假设它是。

However it still is going ahead and check your validation and showing up on the client side through the validation summary. 但是它仍在继续检查您的验证并通过验证摘要显示在客户端。

If you really don't care about the errors in this case try 如果你真的不在乎这种情况下的错误试试

ModelState.Clear()

and remove all the errors out of it. 并删除所有错误。

The server-side validation must be in your MyViewModel class. 服务器端验证必须位于MyViewModel类中。 Can you use a different class that does not implement the validation? 您可以使用不实现验证的其他类吗? The data annotations in the ViewModel is responsible for setting ModelState.IsValid to false. ViewModel中的数据注释负责将ModelState.IsValid设置为false。

Now, I just had this idea: 现在,我有这个想法:

...
else // if "OK" button has been clicked
{
    ModelState.Clear();
}
...

Indeed I don't get messages in ValidationSummary anymore. 事实上,我不再在ValidationSummary中收到消息了。 Does this have any downside or undesirable side effect? 这是否有任何缺点或不良副作用? At least I cannot see an issue at the moment... 至少我现在看不到问题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM