我在尝试将值设置为登录到网站的用户时出现问题。这是我到目前为止,但我得到了这个错误 错误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);
}
答案 0 :(得分:1)
无法将属性或索引器'Employee.getName'分配给 - 它是 只读。
错误是不言自明的。在您的Employee
课程中,您已为此属性定义了getName
只有get
访问者方法。这意味着,它的值只能通过其他一些代码读取。您正在尝试将值设置为此属性,因此编译器正在抱怨它。
如果您希望某个其他代码可以设置此属性的值,则此属性上应该有set
access modifier。
我建议你在视图模型中有一个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;