C# mvc3登录表单错误的用户名和密码消息

C# mvc3登录表单错误的用户名和密码消息,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,和LoginModel [HttpPost] public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { Authenticate(model.Username, model.Password); return RedirectToAction("Index");

和LoginModel

[HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                Authenticate(model.Username, model.Password);
                return RedirectToAction("Index");
            }
            else
            {
                return View(model);
            }
        }

        private void Authenticate(string userName, string password)
        {
            const string commaSeperatedRoles = "Administrator,Editor";

            if (userName == "xx" && password == "xxx")
            {
                FormsAuthenticationUtil.RedirectFromLoginPage(userName, commaSeperatedRoles, false);
            }
        }
和视图

public class LoginModel
    {
        [Required(ErrorMessage="*")]
        [StringLength(10)]
        public string Username { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(10)]
        public string Password { get; set; }
    }
@使用(Html.BeginForm())
{
用户名:
@TextBoxFor(m=>m.Username,新的{@style=“width:140px;”})
密码:
@Html.PasswordFor(m=>m.Password,新的{@style=“width:140px;”})
错误的用户名和密码

}

但现在我总是收到这样的信息:错误的登录名“错误的用户名和密码”。只有当用户名和密码错误时,我如何才能写此消息?我在C#中使用mvc3。我是否可以发送bool变量以供查看,或者sbet的方式是什么?

如果凭据错误,请向模型状态添加错误消息:

@using (Html.BeginForm())
{
    <table width="100%">
    <tr>
        <td>Username:</td>
        <td>
            @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
        </td>
    </tr>
    <tr>
        <td>Passwd:</td>
        <td>
            @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
        </td>
    </tr>
    </table>
    <div class="napaka">Wrong username and password</div>
    <br />
    <input type="submit" class="button" value="Login" />
}
在视图中,使用ValidationSummary帮助程序,而不是在div中硬编码错误消息:

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
        if (Authenticate(model.Username, model.Password))
        {
            // authentication was successful. The method created the cookie
            // so we can safely redirect to an authenticated page here
            return RedirectToAction("Index");
        }
        else
        {
            // authentication failed due to wrong username or password =>
            // we add an error message to the ModelState here so that we 
            // can show it in the view
            ModelState.AddModelError("", "Wrong username and password");
        }
    }

    // At this stage we know that some error happened =>
    // we redisplay the view so that the user can fix it
    return View(model);
}

private bool Authenticate(string userName, string password)
{
    const string commaSeperatedRoles = "Administrator,Editor";
    if (userName == "xx" && password == "xxx")
    {
        // Warning: you should not be redirecting here. You should only
        // create and set the authentication cookie. The redirect should be done
        // in an MVCish way, i.e. by returning a RedirectToAction result if this
        // method returns true
        FormsAuthenticationUtil.SetAuthCookie(userName, commaSeperatedRoles, false);
        return true;
    }

    // wrong username or password
    return false;
}
@使用(Html.BeginForm())
{
用户名:
@TextBoxFor(m=>m.Username,新的{@style=“width:140px;”})
密码:
@Html.PasswordFor(m=>m.Password,新的{@style=“width:140px;”})
@Html.ValidationSummary(false)

}
如果需要帮助,请发布LoginModel类的内容;)@senzacionale,是的,这就是ValidationSummary助手的功能。因此,您只需将自定义类替换为
验证摘要错误
,就可以开始了。另一种可能是编写一个自定义帮助程序,它将获取第一条错误消息并将其显示在视图上,但是您真的需要这样的东西吗?我的意思是,遵守惯例似乎更好。但现在我总是得到,我不能使用我的css类
@using (Html.BeginForm())
{
    <table width="100%">
    <tr>
        <td>Username:</td>
        <td>
            @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
        </td>
    </tr>
    <tr>
        <td>Passwd:</td>
        <td>
            @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
        </td>
    </tr>
    </table>

    @Html.ValidationSummary(false)
    <br />
    <input type="submit" class="button" value="Login" />
}