MVC登录
建立Models
public int Id { get; set; }
[DisplayName("用户名")]
public string UserName { get; set; }
[DisplayName("密码")]
[DataType(DataType.Password)]
public string Password { get; set; }
创建上下文类
public class MsgContext:DbContext
{
public DbSet<Message> Messages { get; set; }
public DbSet<Login> Admins { get; set; }
}
还需要在Web.Config中改变数据库连接
控制器代码
ColContext db = new ColContext();
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Login User)
{
var user = db.Logins.Where(a => a.UserName == User.UserName && a.Password == User.Password).FirstOrDefault();
if (user != null)
return RedirectToAction("Index", "Admin");
return View();
}
添加视图(选择Login,Edit)
@model MyObject.Models.Login
@{
ViewBag.Title = "Login";
}
<h2>留言管理登录</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>登录</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="登录" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}