我正在尝试使用visual studio 2012在mvc4中创建登录页面,但我在这里收到一个问题。
问题是,当我按下“登录”按钮时,它只刷新页面并再次显示登录页面而不是用户登录。
我的代码在这里:
登录视图
@model onlinebookstore.entityframwork.customer
@{
ViewBag.Title = "Login";
Layout = "~/Views/shared/layout.cshtml";
}
<h2>Login</h2>
<div style="background-color:brown;">
@using (Html.BeginForm("Login","Account",FormMethod.Post)){
@Html.LabelFor(model =>model.user_id)
@Html.TextBoxFor(model =>model.user_id)
@Html.LabelFor(model => model.password);
@Html.PasswordFor(model => model.password);
<input type="submit" name="btnsubmit" id="btnsubmit" value="Login" />
}
</div>
帐户控制器
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(onlinebookstore.entityframwork.customer customer)
{
if (ModelState.IsValid)
{
onlinebookstore.entityframwork.onlinebookstoreEntities db = new onlinebookstoreEntities();
var v = db.customers.FirstOrDefault(u => u.user_id == customer.user_id && u.password == customer.password);
if (v != null)
{
Session["userid"] = v.user_id.ToString();
Session["firstname"] = v.first_name.ToString();
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("Register", "Account");
}
}
return View(customer);
}
在此之前,这段代码工作正常,但现在它给我带来了严重的问题请帮助我。
谢谢Rizwan
答案 0 :(得分:0)
检查您的ModelState.Isvalid是真还是假。