Я работаю над приложением, в котором учащиеся выбирают свой отдел и роль при регистрации на сайте. Мне удалось добавить возможность выбора отдела и роли в регистрационную форму.
На самом деле DropDownList для отдела заполняется из таблицы отдела в базе данных. но когда я пытаюсь зарегистрировать пользователя после заполнения формы, я получаю это сообщение об ошибке
Ошибка сервера в «/» приложении. Оператор INSERT противоречит
ограничение FOREIGN KEY
"FK_dbo.AspNetUsers_dbo.Departments_DepartmentID". Конфликт
произошла в базе данных "aspnet-AptechAPP-20180514123201", таблица
«dbo.Departments», столбец «DepartmentID». Заявление было
прекращено.
Ниже приведен снимок экрана моего RegisterViewModel
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { 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 DepartmentID { get; set; }
public IEnumerable<System.Web.Mvc.SelectListItem> DepartmentList
{
get; set;
}
}
Мой RegisterViewController
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
.ToList(), "Name", "Name");
RegisterViewModel model = new RegisterViewModel();
ConfigureRegisterViewModel(model);
return View(model);
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
ConfigureRegisterViewModel(model);
return View(model);
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, DepartmentID = model.DepartmentID };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
.ToList(), "Name", "Name");
AddErrors(result);
// If we got this far, something failed, redisplay form
ConfigureRegisterViewModel(model);
return View(model);
}
private void ConfigureRegisterViewModel(RegisterViewModel model)
{
IEnumerable<Department> departments = db.Departments.OrderBy(u => u.DepartmentName).ToList();
model.DepartmentList = departments.Select(a => new SelectListItem
{
Value = a.DepartmentID.ToString(),
Text = a.DepartmentName.ToString()
});
}
Моя DepartmentViewModel
public class Department
{
public virtual int DepartmentID { get; set; }
public virtual string DepartmentName { get; set; }
}
My IdentityModel
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public int DepartmentID { get; set; }
public virtual Department Department
{
get; set;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Register.Cshtml
<div class="form-group">
@Html.LabelFor(model => model.DepartmentID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.DepartmentID, Model.DepartmentList, "Please select", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.Label("user Role", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@*@Html.DropDownList("Name")*@
@Html.DropDownList("UserRoles", (SelectList)ViewBag.Name, " ")
</div>
</div>