必需属性没有asp.net的MVC工作 [英] required attribute not working in asp.net mvc

查看:170
本文介绍了必需属性没有asp.net的MVC工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的强类型的视图。

I have a simple strongly-typed view.

@model GoldForGold.Models.LogonModel
@{
    ViewBag.Title = "Logins";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Logins
@using (Html.BeginForm()) {

Account Information
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id = "txtUserName" })
@Html.ValidationMessageFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtPassword" })
@Html.ValidationMessageFor(m => m.Password)
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)

<input type="submit" value="Log On" onclick="getcredentials()" />
}

示范code是在这里。

Model code is here.

public class LogonModel
{
    [Required(ErrorMessage="please enter username")]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

我看没有什么,甚至当我不输入用户名和密码发生的事情。

I see nothing is happening even when I do not enter username and password.

推荐答案

有关客户端验证你需要jQuery和jQuery验证。

For client side validation you need jquery and jquery validation.

之后的客户端验证必须配置启用:

after that client side validation must be enabled in config:

<appSettings> 
...
<add key="ClientValidationEnabled" value="true" /> 
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

服务器端验证您可以检查您的验证状态:

for server side validation you can check your validation state with:

ModelState.IsValid;

这篇关于必需属性没有asp.net的MVC工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆