Reputation: 47
Given below was my code, and when i am login in form at time error will occur is that " Object reference not set to an instance of an object. ".Actually i m display data in master page.
Master page:-
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<%@ Import Namespace="ClientProj.Models" %>
<%foreach(var m in (IEnumerable<user_master>)ViewData["email"])
{ %>
<%=m.email %>
<%} %>
And My Controller :-
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();
}
Upvotes: 2
Views: 952
Reputation: 30618
You're setting ViewData in your controller in one method, but trying to read it out in the master page for ANY page. This means you'll need to ensure that every action you have sets the Viewdata, which is really a bad idea.
What's probably happening here is that you've got another action which isn't setting the ViewData, such as the HttpPost version of Index.
Upvotes: 2