我正在验证用户名和密码,但它不能在mvc4中工作

时间:2016-08-19 08:14:03

标签: asp.net-mvc asp.net-mvc-4

这是我的控制器

public ActionResult Index(Models.boss ob)
{
    ViewBag.Bid = new SelectList(db.bosses, "Bid", "username");
     var mm = db.bosses.Any(model => model.username == ob.username && model.password == ob.password);
     if (mm)
     {
         return RedirectToAction("welcome");
     }
     else
     {
         ModelState.AddModelError("", "Login data is incorrect!");
     }
     return View(ob);
 }

这是我的Index.View我验证了所有内容,但在这样的网页上显示相同

  

登录失败。检查您的登录详细信息登录数据不正确!

那我做错了什么?

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "Login failed. Check your login details.");
    <div class="editor-field">
        @Html.LabelFor(model => model.username)
        @Html.EditorFor(model => model.username)
        @Html.ValidationMessageFor(model => model.username)
    </div>
    <div>
        @Html.ValidationMessageFor(m => m.username)
    </div>
    <div class="editor-field">
        @Html.LabelFor(model => model.password)
        @Html.PasswordFor(model => model.password, "password=*")
    </div>
    <div>
        <label>SELECT USER</label>
        @Html.DropDownList("boss", ViewBag.Bid as SelectList, "Select a User", new { id = "boss" })
    </div>
    <button>LOGIN</button>
}

1 个答案:

答案 0 :(得分:0)

服务器端错误"Login data is incorrect!",写在控制器中。 客户端您已在ValidationSummary中设置了初始错误"Login failed. Check your login details."

请删除初始客户端验证消息。

您还必须在动作调用中传递 [ValidateAntiForgeryToken] 令牌参数。

请查看详细信息:https://stackoverflow.com/a/38893962/6606630