Asp.net mvc 3 无法创建类型为';MVCAPApplication4.Models.Users';。仅支持基本类型(';如Int32、String和Guid';)
我有一个员工屏幕和一个拥有员工外键的用户实体。如果为任何员工创建了用户,则不应在员工下拉列表中加载该员工。我收到错误“无法创建常量值” 这是我的两个类雇员和用户Asp.net mvc 3 无法创建类型为';MVCAPApplication4.Models.Users';。仅支持基本类型(';如Int32、String和Guid';),asp.net-mvc-3,Asp.net Mvc 3,我有一个员工屏幕和一个拥有员工外键的用户实体。如果为任何员工创建了用户,则不应在员工下拉列表中加载该员工。我收到错误“无法创建常量值” 这是我的两个类雇员和用户 public class EmployeeMaster { [Key, System.ComponentModel.DisplayName("Employee ID")] public int EmployeeID { get; set; } public stri
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;
}
}
}
这是我的视图代码,在下拉列表中插入值
<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>
雇主主管
@Html.DropDownListFor(model=>model.EmployeeMasterId,model.SelectedListItem.Select(option=>newselectListItem
{
Text=(option==null?“无”:option.FullName),
Value=option.EmployeeID.ToString(),
选中=(Model!=null)&&(option.EmployeeID==Model.EmployeeMasterId)
})"依扎贝利特",;
@Html.ValidationMessageFor(model=>model.EmployeeMasterId)
我在创建视图时遇到此错误 我试图编辑这个问题,看它是否更清晰,但我不明白创建新EmployeeUser时您想做什么。我建议您澄清您的问题,以便获得一些帮助。您好,我正在尝试筛选用户屏幕上的Employee Master下拉列表,因为我已经创建了一个Employee Master的IEnumarable集合,但当我尝试创建一个新的Employee用户时,它会抛出一个异常“”无法创建ModelApplication4.Models.Users类型的常量值,在此上下文中仅支持基元类型。问题是当您尝试显示下拉列表时?或者下拉列表显示正确,问题是当您即将创建新用户时?问题是当我即将创建新用户时,它在代码行下方显示下拉列表时引发异常:@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”);在获取EmployeeMasters列表时,请尝试使用context.EmployeeMasters.ToList(),并告诉我。
<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>