我试图登录我的网站,但是当我这样做时我遇到了一些问题
这是来自控制器的登录代码:
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogIn(Accounts U)
{
if (ModelState.IsValid)
{
if (IsValid(U.Username, U.Password))
{
FormsAuthentication.SetAuthCookie(U.Username, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Invalid Username or Password");
}
}
else
{
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
ModelState.AddModelError("", errors.ToString());
}
return View(U);
}
这是我得到的错误:
<> f__AnonymousType1`2 [System.String,System.Web.Mvc.ModelErrorCollection] []
我还修改了一个来自互联网的视图,但仍然是相同的结果。
PS:注册工作没有任何问题
修改
我改变了这个:
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
进入这个:
var errors = ModelState.Values.SelectMany(v => v.Errors);
这是错误:
System.Linq.Enumerable + d__16`2 [System.Web.Mvc.ModelState,System.Web.Mvc.ModelError]
帐户模型:
public partial class Accounts
{
public string Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[NotMapped]
public string ConfirmPassword { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}
答案 0 :(得分:0)
好的,我解决了这个问题。
我做的是从帐户模型中删除EmailAddress中的[必需]