![](/img/trans.png)
[英]MVC3 Unobtrusive Validation not working for custom DataAnnotations attribute
[英]In MVC3, How can the controller know the result of the unobtrusive validation?
我正在使用C#
, MVC3
和VS2010
我注意到,即使校驗結果為false,控制器方法仍然會執行。 這使得驗證在服務器端無用。 除非有辦法得到結果。
----編輯-----
這是我如何使用它。 做得好嗎? 至少會顯示驗證消息,並且文本框將變為紅色。
模型:
public class CategoriaModel
{
[Required(ErrorMessage="Nome é obrigatório!")]
[StringLength(10, ErrorMessage = "First Name max length is 10")]
public string Nome { get; set; }
}
視圖:
@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" />
}
控制器:
public ActionResult Salvar(CategoriaModel catModel)
{
ViewBag.StatusMessage = "Ok!";
return View("Index");
}
測試ModelState.IsValid
屬性,但是如果客戶端驗證失敗,則不應調用該Action。 檢查您是否實際上正在驗證所有Model屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.