如何让用户登录

时间:2017-11-14 15:02:07

标签: asp.net-mvc

我在尝试将值设置为登录到网站的用户时出现问题。这是我到目前为止,但我得到了这个错误 错误CS0200属性或索引器' Employee.getName'无法分配 - 它是只读的。我将对设置登录视图的用户进行哪些更改

员工模型

 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;
        }
    }


}

}

控制器

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);
    }

1 个答案:

答案 0 :(得分:1)

  

无法将属性或索引器'Employee.getName'分配给 - 它是   只读。

错误是不言自明的。在您的Employee课程中,您已为此属性定义了getName只有get访问者方法。这意味着,它的值只能通过其他一些代码读取。您正在尝试将值设置为此属性,因此编译器正在抱怨它。

如果您希望某个其他代码可以设置此属性的值,则此属性上应该有set access modifier

恕我直言,你应该保持你的视图模型简洁。不应该有任何数据访问代码来获取视图模型属性中的数据(即将2个关注点,UI和数据访问混合在一起!)

我建议你在视图模型中有一个settable和gettable属性来传递登录的用户名

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

现在您可以根据需要设置

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

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