我对MVC很安静。我正在创建用户管理模块,用于对使用该应用程序的用户进行身份验证和授权。因此,我创建了两个模型,一个用于用户角色,另一个用于用户详细信息。
我创建了两个这样的模型:
public class RolesModels
{
public RolesModels()
{
this.Users = new HashSet<UserModels>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RoleId { get; set; }
[Required]
[DataType(DataType.Text)]
[StringLength(20, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
[Display(Name = "Caption")]
public string Caption { get; set; }
[Display(Name = "Can Create")]
public bool createRole { get; set; }
[Display(Name = "Can View")]
public bool viewRole { get; set; }
[Display(Name = "Can Modify")]
public bool modifyRole { get; set; }
[Display(Name = "Can Delete")]
public bool deleteRole { get; set; }
public virtual ICollection<UserModels> Users { get; set; }
}
public class UserModels
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int user_id { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "User Name")]
[StringLength(20, ErrorMessage = "The {0} must be at least 3 characters long.", MinimumLength = 3)]
public string user_name { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[StringLength(10, ErrorMessage = "The {0} must be at least 4 characters long.", MinimumLength = 4)]
public string user_pass { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
[StringLength(50, ErrorMessage = "The {0} must be at least 6 characters long.", MinimumLength = 6)]
public string UserEmail { get; set; }
[Display(Name = "Registeration Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime RegDate { get; set; }
[Display(Name = "Enable")]
public bool status { get; set; }
[Required]
public int RoleId { get; set; }
[Display(Name = "Roles")]
public virtual RolesModels Roles { get; set; }
}
我的注册控制器操作如下:
[HttpGet]
public ActionResult registration()
{
// var md = from m in db.RolesModels
// orderby m.Caption descending
// select m;
// Html.DropDownList("Roles", new SelectList(ViewBag.Roles), new { @id = "dropdown" });
ViewBag.Roles = db.RolesModels.Select(r => r.Caption);
ViewBag.RolesId = db.RolesModels.Select(r => r.RoleId);
return View();
}
我使用下拉列表创建了注册视图,如下所示:
@Html.DropDownList("RoleId", new SelectList(ViewBag.Roles), new { @id = "dropdown" })
我的HTTPPost就在这里
[HttpPost]
public ActionResult registration(Solnet_HotelSuite.Models.UserModels user)
{
if (ModelState.IsValid)
{
using (var db = new DBEntity())
{
var sysUser = db.UserModels.Create();
sysUser.user_name = user.user_name;
sysUser.user_pass = user.user_pass;
sysUser.UserEmail = user.UserEmail;
sysUser.RegDate = user.RegDate;
sysUser.RoleId = user.RoleId;
//sysUser.Roles = user.Roles;
sysUser.status = user.status;
db.UserModels.Add(sysUser);
db.SaveChanges();
}
}
return View(user);
}
但每当我点击注册用户时,我都会收到错误:
值不能为空。参数名称:items
我做错了什么?
答案 0 :(得分:1)
回发后,您放入viewbag中的项目将丢失。尝试将HttpPost代码的结尾更改为:
ViewBag.Roles = db.RolesModels.Select(r => r.Caption);
ViewBag.RolesId = db.RolesModels.Select(r => r.RoleId);
return View(user);
您可能需要调整此代码,因为此时db
对象可能超出范围。