Why doesn't client validation work?
<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.min.js")%>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/MicrosoftAjax.js")%>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/MicrosoftMvcValidation.js")%>" type="text/javascript"></script>
<h2>Criar</h2>
<h2>Login</h2>
Faça login aqui
<% Html.EnableClientValidation(); %开发者_运维知识库>
<% using (Html.BeginForm())
{ %>
<%: Html.ValidationSummary(false) %>
<%: Html.LabelFor(x => x.Nome) %><%: Html.EditorFor(x => x.Nome) %>
<%: Html.LabelFor(x => x.Email) %><%: Html.EditorFor(x => x.Email) %>
<%: Html.LabelFor(x => x.Senha) %><%: Html.EditorFor(x => x.Senha) %>
<%: Html.LabelFor(x => x.ConfirmacaoSenha) %><%: Html.EditorFor(x => x.ConfirmacaoSenha) %>
<input type="submit" value="Criar Conta" />
<% } %>
This is the model class:
public class CadastroViewModel
{
[Required(ErrorMessage = "Por favor digite seu nome")]
public string Nome { get; set; }
[Required(ErrorMessage = "Por favor digite seu e-mail")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Por favor entre com um e-mail válido")]
public string Email { get; set; }
[Required(ErrorMessage = "Por favor digite sua senha")]
public string Senha { get; set; }
[Required(ErrorMessage = "Por favor confirme sua senha")]
[DisplayName("Confirmação de Senha")]
public string ConfirmacaoSenha { get; set; }
}
You need to add the ValidationMessageFor helper to enable client side validation:
<%: Html.LabelFor(x => x.Nome) %>
<%: Html.EditorFor(x => x.Nome) %>
<%: Html.ValidationMessageFor(x => x.Nome) %>
<%: Html.LabelFor(x => x.Email) %>
<%: Html.EditorFor(x => x.Email) %>
<%: Html.ValidationMessageFor(x => x.Email) %>
<%: Html.LabelFor(x => x.Senha) %>
<%: Html.EditorFor(x => x.Senha) %>
<%: Html.ValidationMessageFor(x => x.Senha) %>
<%: Html.LabelFor(x => x.ConfirmacaoSenha) %>
<%: Html.EditorFor(x => x.ConfirmacaoSenha) %>
<%: Html.ValidationMessageFor(x => x.ConfirmacaoSenha) %>
ValidationSummary does not work with Client Validation. You'll have to add the ValidationMessageFor each element.
e.g.
<%: Html.LabelFor(x => x.Nome) %><%: Html.EditorFor(x => x.Nome) %> <%: Html.ValidationMessageFor(x => x.Nome)%>
精彩评论