I have three action methods and public field public int idUser:
public class HomeController : Controller
{
//public field
public int idUser = 0;
public ActionResult Index()
{
string userLogin = User.Identity.Name;
using (SmoothEntities db = new SmoothEntities())
{
idUser = (db.Users.Where(c => c.Name == userLogin).First()).ID_User;
return View(db.Employees.Where(u => u.ID_User == idUser).ToList());
}
}
public ActionResult About()
{
string userLogin = User.Identity.Name;
int idUser = 0;
using (SmoothEntities db = new SmoothEntities())
{
idUser = (db.Users.Where(c => c.Name == userLogin).First()).ID_User;
return View(db.Employees.Where(u => u.ID_User == idUser).ToList());
}
}
public ActionResult Contact()
{
return View();
}
}
Let us imagine that Bob and Bill have logged simultaneously on to the web site. Am I right that Bob and Bill will have two different values of the public field idUser if Bob will execute public ActionResult Index() and Bill will execute public ActionResult About()?
Why do I use public field? As I do not want always access data from server in Action methods, I think it will decrease performance of my web site, so I have decided to create public field.