Привет, мы разрабатываем веб-приложение с использованием mvc4 и jquery mobile. В нашем контроллере мы создали пользовательский сеанс, как показано ниже.Признание значения из глобальной переменной в MVC
общественный класс HomeController: BaseController { private static User UserUser;
public ActionResult Index(string name)
{
CurrentUser = (User)Session["CurrentUserSession"];
return View();
}
public ActionResult UserDetaiks()
{
string username = CurrentUser.UserFName;
return View()
}
}
Above we created object for User model and assigned session value in index method. But the value in CurrentUser is lost once i entered UserDetails. So i Used static while creating object. My question is it correct? or anyother way is there. Please guide me.
Спасибо, ребята еще один Сомнение,
Я использовал ниже код в виде Authenticate.
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
User obj = s.Deserialize<User>(authTicket.UserData);
UserInformation CurrentUser = new UserInformation(obj.UserID);
CurrentUser.UserId = obj.UserID;
CurrentUser.FirstName = obj.UserFName;
CurrentUser.LastName = obj.UserLName;
CurrentUser.roles = obj.SecurityGroupName;
CurrentUser.DefaultWarehouseId = obj.DefaultWhseIdentity; eg:14
HttpContext.Current.User = CurrentUser;
}
**Here User can Change CurrentUser.DefaultWarehouseId later like CurrentUser.DefaultWarehouseId = 16. but when i leave the method. It again getting value 14. Now i can code so CurrentUser.DefaultWarehouseId will be 16 through out app one i changed, Please guide me.**
почему вы создаете это глобальный вы можете получить его из сессии в действие локально при необходимости –
Кроме того, нет CurrentUser, если вы посещаете детали непосредственно, не посещая индекс первого, потому что нет никакого присваивания происходит. – Marco
Пробовал ли вы это с двумя разными пользователями? Использование 'static' не будет работать. – CodeCaster