日期:2014-05-17  浏览次数:20554 次

页面的提交
1.是不是页面response的时候,就把LogOnModel Attribute[require]映射到 cshtml上?
2.cshtml上的@Html.TextBox要保持对应,就必须让cshtml的@Html.TextBox("username")中的名称和LogOnModel 中的一致就行??
3.提交表单request后,@Html.TextBox的require验证信息,是客户端验证提示的,还是服务器返回的?  



cshtml.cs
C# code

@{
    ViewBag.Title = "后台管理登录";
}
@section Header{
    <script type="text/javascript">
        $(function () { $("#username").focus(); });</script>
    <style type="text/css">
        input[type="text"], input[type="password"]
        {
            width: 160px;
        }
    </style>
}
<h2>
    登录</h2>
@Html.ValidationSummary("登录失败,无效的用户名或密码")
@using (Html.BeginForm())
{ 
    <div style="width: 380px; margin: 0px auto">
        <fieldset>
            <legend>帐号信息</legend>
            <p>
                <label for="username">
                    帐号:</label>
                @Html.TextBox("username")
                @Html.ValidationMessage("username")
            </p>
            <p>
                <label for="password">
                    密码:</label>
                @Html.Password("password")
                @Html.ValidationMessage("password")
            </p>
            <p>
                @Html.CheckBox("rememberMe")
                <label class="inline" for="rememberMe">
                    下次自动登录(公共电脑上谨慎使用)</label>
            </p>
            <div class="center">
                <input type="submit" value="登录(L)" accesskey="L" />
            </div>
            <p>@Html.ActionLink("还没有账号?立即注册", "Register")</p>
        </fieldset>
    </div>
}




controller action
C# code

        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)//???
            {
                RegisteredUser usr = RegisteredUserBLL.AuthenticateUser(model.UserName, model.Password);
                if (usr != null)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName + "|" + usr.RealName + "|" + usr.UserType, model.RememberMe);  
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))       写????
                        return Redirect(returnUrl);                
                    return RedirectToAction("Index", "Home");       
                }
                ModelState.AddModelError("", "登录账号或密码不正确");  
            }
            return View(model);
        }




C# code

    public class LogOnModel
    {
        [Required]
        [Display(Name = "账号")]
        public string UserName { get; set; }

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

        [Display(Name = "记住登录信息")]
        public bool RememberMe { get; set; }
    }