Use of Sessions in C #

0

I am working on a Web project and I implement Sessions for the user login that changes the view depending on the role of the user, but I have the problem that the view changes when it is a single user of all there is, I made tests and the users do exist in my data context, but as I said, only the view changes when it is a specific user, when I try to send data from another user, the error appears. image.

Part of my code is this way

var user = db.User.Where(u => u.UserName.Equals(obj.UserName) && 
           u.Password.Equals(obj.Password)).FirstOrDefault();

if(user != null){
     Session["UsuarioId"] = use.UsuarioId.ToString();
     Session["UserName"] = use.UserName.ToString();
     return RedirectToAction("LoggedIn");
}
else{
     ModelState.AddModelError("","Invalid UserName or Password");
}
    
asked by Jose Campos 14.05.2017 в 05:49
source

1 answer

0

Some characteristics to keep in mind of @RenderBody() :

  • RenderBody must be present in the parent design view, it is at this point where it fails, it does not have the render added to it ( Layout.cshtml )
  • RenderBody does not include any parameters

Basic example

Layout.cshtml

<!DOCTYPE HTML>
<html>
<head>
    <title>S.O</title>
</head>
<body>
    <h2>Texto del Padre</h2>
    @RenderBody() <!-- Llamada a Render , si no salta el error-->
</body>
</html>

LoggedIn.cshtml

@{
  Layout = "~/Views/Shared/_Layout.cshtml"; /* Padre*/
}
 <p> Hola SOeS , Desde Vista Hija</p>

Result

  

Father's Text

     

Hello SOeS, From Vista Daughter

    
answered by 14.05.2017 в 10:16