従業員の画面が 1 つと、従業員の外部キーを持つユーザー エンティティが 1 つあります。For any employee ユーザーが作成された場合、その従業員は Employee Dropdown に読み込まれません。「定数値を作成できません」というエラーが表示されます。
これは私の2つのクラス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>
ビューの作成中にこのエラーが発生しました。