现在的位置: 首页 > 综合 > 正文

ASP.NET MVC数据验证 登录 注册用 学习留用

2018年07月07日 ⁄ 综合 ⁄ 共 710字 ⁄ 字号 评论关闭

1.导入js   

 jquery.js   

 jquery.validate.min.js 

 jquery.validate.unobtrusive.min.js

2.先定义一个视图模型类 需要引用System.ComponentModel.DataAnnotations.dll

public class LoginUser
{
    [Required]//表示必须
    public string LoginName { get; set; }
    [Required(ErrorMessage = "密码不能为空!")]
    public string Pwd { get; set; }
    public bool IsAlways { get; set; }
}

3.把视图设为强类型

@model  LoginUser//全名称

视图里写


<table>
            <tr>
                <td>用户名:</td>
                <td>@Html.TextBoxFor(u => u.LoginName)</td>
                <td>@Html.ValidationMessageFor(u => u.LoginName)</td>//验证失败显示消息
            </tr>
            <tr>
                <td>密码:</td>
                <td>@Html.PasswordFor(u => u.Pwd)</td>
                <td>@Html.ValidationMessageFor(u => u.Pwd)</td>
            </tr>
            <tr>
                <td>@Html.CheckBoxFor(u => u.IsAlways)</td>
                <td colspan="2"><input type="submit" value="登陆" /></td>
            </tr>
        </table>

System.ComponentModel.DataAnnotations 命名空间

抱歉!评论已关闭.