Как получить доступ к свойству из другого класса C # / MVC - PullRequest
0 голосов
/ 03 марта 2020

В основном проблема в том, что мне нужен доступ к идентификатору текущего пользователя в классе. Я получаю это в MVC контроллере хорошо, но мне нужно иметь возможность доступа в классе. Поэтому я пытаюсь установить его с помощью свойства, но не могу понять, получить часть. Вот код:

//Employee.cs  
public class CurrUser
    {
        public string USERID{ get; set; }

    }

MVC Контроллер:

//HomeController
        public async Task<JsonResult> GetDefaultMgrGeid()
        {
            string Soeid = User.Identity.Name.Split("\\".ToCharArray())[1];
            //setting prop here
            CurrUser emp = new CurrUser();
            emp.USERID = Userid;

            var task = Task.Run(() => DAL.Employee.GetDefaultMgr(Soeid));

            var retData = await task;
            return new JsonResult
            {
                ContentType = "application/json",
                Data = retData,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = int.MaxValue
            };
        }

Мне нужно получить доступ (получить) значение здесь:

public class HR
    {
    // I tried this but did not work either
        // public CurrUser user{ get; set; }
        //  public string GetUser
        //  {
        //      get
        //     {
        //          return user.USERID;
        //     }
        //  }
       public static IEnumerable<DTO.Employee> GetGlobalMgrs()
        {
         string mrgID = CurrUser.??;// I can't access it here
        }

}

Пожалуйста, руководство !

1 Ответ

0 голосов
/ 04 марта 2020

Измените его на:

    public class HR
    {
       //by setting this property to static your static methods in this class can access it
       public static CurrUser user{ get; set; }

       public static IEnumerable<DTO.Employee> GetGlobalMgrs()
       {
         string mrgID = user.USERID;
         //put your other code here like retrieving the list
       }
    }

Если вам нужен доступ к нему из другого класса:

сделайте это: Hr.user Убедитесь, что вы using правильное пространство имен

...