下面给出的是我的代码,当我在表单中登录时会发生错误是“对象引用未设置为对象的实例。”。实际上我在母版页中显示数据。
母版页:-
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<%@ Import Namespace="ClientProj.Models" %>
<%foreach(var m in (IEnumerable<user_master>)ViewData["email"])
{ %>
<%=m.email %>
<%} %>
还有我的控制器:-
public ActionResult Index()
{
ViewData["email"] = from p in db.user_master select p;
return View();
}
[HttpPost]
public ActionResult Index(user_master log)
{
ViewData["email"] = from p in db.user_master where
p.user_id==Convert.ToInt32(Session["user"]) select p;
var ss = from p in db.user_master
where p.username == log.username &&
p.user_password == log.user_password
select p;
if (ss.Count() > 0)
{
Session["id"] = ss.First().user_id;
Session["user"] = ss.First().username;
return RedirectToAction("Home");
}
else
{
return RedirectToAction("index");
}
return View();
}