简体   繁体   English

在MVC3中,控制器如何知道不干扰验证的结果?

[英]In MVC3, How can the controller know the result of the unobtrusive validation?

I'm using C# , MVC3 and VS2010 我正在使用C#MVC3VS2010

I noticed that even though the valdiation results to false, the controller method still gets executed. 我注意到,即使校验结果为false,控制器方法仍然会执行。 This makes the validation useless on the server-side. 这使得验证在服务器端无用。 Unless there is a way to get the result. 除非有办法得到结果。

---- EDITS ----- ----编辑-----
Here is how i'm using it. 这是我如何使用它。 Is it done properly? 做得好吗? At least the validation messages show up, and the textbox turns red. 至少会显示验证消息,并且文本框将变为红色。

Model: 模型:

public class CategoriaModel
{
    [Required(ErrorMessage="Nome é obrigatório!")]
    [StringLength(10, ErrorMessage = "First Name max length is 10")]
    public string Nome { get; set; }



}

View: 视图:

@Html.ValidationSummary(true)
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<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("Salvar", "Test", FormMethod.Post))
{


    @Html.LabelFor(m => m.Nome)
    @Html.EditorFor(m => m.Nome)
    @Html.ValidationMessageFor(m=>m.Nome)                           


    <input type="submit" value="Salvar" />
}

Controller: 控制器:

public ActionResult Salvar(CategoriaModel catModel)
        {

            ViewBag.StatusMessage = "Ok!";



            return View("Index");
        }

testing ModelState.IsValid property, however if client side validation fails the Action should not be called. 测试ModelState.IsValid属性,但是如果客户端验证失败,则不应调用该Action。 Check if you're actually validating all your Model properties. 检查您是否实际上正在验证所有Model属性。

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

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