0

我有一个员工屏幕和一个具有员工外键的用户实体。如果为任何员工创建了用户,则该员工不应加载到员工下拉列表中。我收到错误“无法创建常量值”。

这是我的两个类 Employee 和 User

    public class EmployeeMaster
    {   
        [Key, System.ComponentModel.DisplayName("Employee ID")]
        public int EmployeeID { get; set; }

        public string Salutation { get; set; }

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

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

        [Display(Name = "Joining Date")]
        public DateTime JoiningDate { get; set; }

        public string FullName
        {
            get { return string.Format("{0} {1} {2}", Salutation, FirstName, LastName); }
        }
    }

    public class Users
    {
        [Required, Key]
        [Display(Name = "User ID")]
        public int UserID { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        public int EmployeeMasterId
        { get; set; }

        private RHDMSSoftwareDataBase context = new RHDMSSoftwareDataBase();

        //This collection retunrs list of employee for which user is not created.***
        public IEnumerable<EmployeeMaster> SelectedListItem
        {
            get
            {
                var user = context.Users.ToList();
                var employee = (from emp in context.EmployeeMasters
                                from use in user
                                where emp.EmployeeID != use.EmployeeMasterId
                                select emp).AsEnumerable();
                return employee;
            }
        }
    }

这是我的控制器方法:

public ActionResult Create()
{
    var UserCollection = new Users();
    return View(UserCollection);
}
//
// POST: /Users/Create
[HttpPost]
public ActionResult Create(Users users)
{

这是我在下拉列表中插入值的视图代码

<div class="editor-label">
            EmployeeMaster
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.EmployeeMasterId, Model.SelectedListItem.Select(option => new SelectListItem
       {
           Text = (option == null ? "None" : option.FullName),
           Value = option.EmployeeID.ToString(),
           Selected = (Model != null) && (option.EmployeeID == Model.EmployeeMasterId)
       }), "Izaberite"));
            @Html.ValidationMessageFor(model => model.EmployeeMasterId)
        </div>

创建视图时出现此错误。

4

0 回答 0