如何在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
}
我发现,我可以通过设置禁用客户端验证类确定按钮=取消
。这工作得很好。
然而,服务器端验证仍然发生。有没有办法在一个控制器动作来禁用它(参见上面的创建行动else区块)?
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)?
感谢您的帮助!
推荐答案
我最近有一个类似的问题。我想排除验证一些性质和使用的下列code:
I recently had a similar problem. I wanted to exclude some properties from validation and used the following code:
ModelState.Remove("Propertyname");
要隐藏errormessages可以使用
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">
这篇关于如何在ASP.NET MVC 3 HttpPost行动禁用验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!