我正在尝试创建一个登录页面,但我的模型状态恢复为无效,我认为这是因为我收到了一个“不完整”的对象。
这是我的控制器
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User u)
{
if (ModelState.IsValid)
{
{
var v = entities.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null)
{
Session["LoggedUserID"] = v.UserID.ToString();
Session["LoggedUserFullname"] = v.FirstName +" "+ v.LastName;
return RedirectToAction("AfterLogin");
}
}
}
return View(u);
}
这是Login
页面本身
@model Car_Dealership.Views.Shared.User
@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{
<div style="border:1px solid red">
@ViewBag.Message
</div>
}
<div>
<fieldset>
<legend>Login</legend>
<div class="editor-label">
@Html.LabelFor(u => u.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(u => u.UserName)
@Html.ValidationMessageFor(u => u.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(u => u.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(u => u.Password)
@Html.ValidationMessageFor(u => u.Password)
</div>
<input type="submit" value="Login" />
</fieldset>
</div>
}
@* This below line is for create javascript section *@
@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
}
这里是User
namespace Car_Dealership.Views.Shared
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class User
{
public User()
{
this.Orders = new HashSet<Order>();
}
[Required]
public int UserID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required(ErrorMessage ="Date Of Birth is Required in YYYY-MM-DD Format")]
[Display(Name = "Date Of Birth")]
[RegularExpression(@"/^\d{4}-\d{2}-\d{2}$/")]
public System.DateTime DateOfBirth { get; set; }
[Required]
public string Gender { get; set; }
[Required(ErrorMessage = "Email is Required")]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
ErrorMessage = "Email is not valid")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
public string Authorize { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
我已经尝试通过执行u
使if(u.username=entities.users.where(a=>a.username)&& the same for password)
拥有它应该对应的对象的所有属性,但这不起作用。不知道该怎么做
答案 0 :(得分:1)
您的假设是正确的:从客户端传递的用户对象未满,并且错过了所需的属性。
解决方案:
您的登录视图不应该收到User
模型而是创建LoginViewModel:
public class LoginViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName {get; set;}
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password {get; set;}
}
你的观点是:
@model LoginViewModel
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{
<div style="border:1px solid red">
@ViewBag.Message
</div>
}
<div>
<fieldset>
<legend>Login</legend>
<div class="editor-label">
@Html.LabelFor(u => u.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(u => u.UserName)
@Html.ValidationMessageFor(u => u.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(u => u.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(u => u.Password)
@Html.ValidationMessageFor(u => u.Password)
</div>
<input type="submit" value="Login" />
</fieldset>
</div>
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel u)
{
if (ModelState.IsValid)
{
{
var v = entities.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null)
{
Session["LoggedUserID"] = v.UserID.ToString();
Session["LoggedUserFullname"] = v.FirstName +" "+ v.LastName;
return RedirectToAction("AfterLogin");
}
}
}
return View(u);
}
P.S。存储普通密码真是个坏主意。您应该存储密码哈希值。我还建议你看一下[asp.net identity 1它真的可以让用户管理变得更容易。