user8869595
user8869595

Reputation:

How to get the user logged in

I'm having an issue with trying to set the value to the user that logged in to the website. This is what I have so far but I'm getting this error Error CS0200 Property or indexer 'Employee.getName' cannot be assigned to -- it is read only. What changes would I make to set the user that logged in the view

Employee Model

 public class Employee
{
    [Required]
    [Display(Name="Employee Number")]
    public int employeeNum { get; set; }

    [Display(Name = "Employee First Name")]
    public string firstName { get; set; }

    [Display(Name = "Employee Last Name")]
    public string lastName { get; set; }

    [Display(Name = "Employee Department")]
    public string department { get; set; }

    [Display(Name = "Employee Name")]
    public string Name
    {
        get
        {


            return string.Concat(firstName, " ", lastName);
        }
    }


    public string getName
    {
        get {
            IssueDAO dbObj = new IssueDAO();
            dbObj.connectionString = "Server=tw-testdb-04;Database=TWCL_OPERATIONS;uid=sa;password=P@ssw0rd";
            var emp= dbObj.getEmployee(employeeNum);
            return emp;
        }
    }


}

}

Controller

private Requisition getRequisition
    {
        get
        {
            Requisition requisition = (Requisition)Session["Requisition"];
            if (requisition == null)
            {
                requisition = new Requisition();
                Session["Requisition"] = requisition;
            }
            return requisition;

        }

    }

 public ActionResult RequisitionItem()
    {
        //Session.Clear();
        //Set the document number and type to autoamtic values
        IssueDAO dbData = new IssueDAO();
        getRequisition.reqDate= DateTime.Now;
        getRequisition.reqNumber= string.Concat("RN", DateTime.Now.ToString("yyyyMMddhhmmssms"));
        getRequisition.count = 0;
        getRequisition.inventory_account = 5520;
        getRequisition.employeeDetails.getName = System.Web.HttpContext.Current.User.Identity.Name;


        getRequisition.item = new Item();

        return View(getRequisition);
    }

Upvotes: 0

Views: 59

Answers (1)

Shyju
Shyju

Reputation: 218702

Property or indexer 'Employee.getName' cannot be assigned to -- it is read only.

The error is self explanatory. In your Employee class, you have defined getName with only get accessor method for this property. That means, the value of it can only read by some other code. You are trying to set the value to this property and hence the compiler is complaining about it.

If you want the value of this property to be settable by some other code, you should have a set access modifier on this property.

IMHO, you should keep your view models lean and simple. There should not be any data access code to get data inside a view model properties ( that is mixing 2 concerns ,UI and Data access together!)

I suggest you have a settable and gettable property in your view model to pass the logged in user name

public class Employee
{
   // Your other properties
   public string LoggedInUserName { set;get;}
}

Now you can set this as needed

var emp=new Employee();
emp.LoggedInUserName = "Any username value here";

or

emp.LoggedInUserName = System.Web.HttpContext.Current.User.Identity.Name;

Upvotes: 1

Related Questions