EF: Why this Model Validation error

تعرفه تبلیغات در سایت
عنوان عکس عنوان عکس عنوان عکس عنوان عکس
عنوان عکس عنوان عکس عنوان عکس عنوان عکس
عنوان عکس عنوان عکس عنوان عکس عنوان عکس
عنوان عکس عنوان عکس عنوان عکس عنوان عکس
عنوان عکس عنوان عکس عنوان عکس عنوان عکس

آرشیو مطالب


یافته ها در جستجو

    امکانات وب

    برچسب ها

    Vote count: 0

    In ASP.NET MVC Core 1.1.1, Individual User Accounts mode, VS2017 Ver 15.3.3, I added some users using Register.cshtml. Later I created an interface to add two roles to ASPNETRoles table. Then I decided to add these users to one of the two Roles. But when I tried, for example, to add Administrator user to AdminRole, I'm getting the following validation error even though the users already have email and password into the ASPNETUsers table. I think the validation error occurs at line var result = await _userManager.AddToRoleAsync(user, "AdminRole"); of the Post action below.


    1. Email is required 2. Password is required

    Note: I think not related to this post - but just in case: I've slightly extended the user model by not using Email as user name; and instead adding an extra attribute UserName to the ApplicationUser.cs class and making some related changes to the Identity model as explained here. But Email is still there as a required attribute and every user does have unique email in ASPNetUsers table


    public class LoginViewModel
        public SelectList lstUsers { get; set; }
        public string SelectedUserName { get; set; } //will be used as selectedUser
        [Display(Name = "Email")]
        public string Email { get; set; }
        public string Password { get; set; }
        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }


    public IActionResult AddUserToRole(string returnUrl = null)
        ViewData["ReturnUrl"] = returnUrl;
        LoginViewModel loginVM = new LoginViewModel();
        loginVM.lstUsers = new SelectList(_userManager.Users.OrderBy(u => u.UserName), "UserName", "UserName");
        return View(loginVM);
    public async Task AddUserToRole(LoginViewModel model, string returnUrl = null)
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
            //var user = _userManager.FindByNameAsync(model.SelectedUserName); //cannot be used in AddToRoleAsync(user,"AdminRole") since FindByNameAsync returns Task
            var user = new ApplicationUser { UserName = model.SelectedUserName };
            var result = await _userManager.AddToRoleAsync(user, "AdminRole");
            if (result.Succeeded)
                return RedirectToLocal(returnUrl);
        // If we got this far, something failed, redisplay form
        return View(model);


    @model LoginViewModel

    Create a new Role.

    @section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } }
    asked 38 secs ago

  • مطالب مرتبط
  • unable to "Deserialise the current Json" error
  • Laravel Model Accessor and Mutator with wildcard (*)
  • How to fix the frame-src - child-src error from firefox in angular 2
  • CGImageDestinationCreateWithURL failed with error "unsupported file format 'public.heic'"
  • Reference Error: Function is not defined.Webpack
  • Meteor Uncaught TypeError: Cannot read property 'helpers' of undefined
  • نویسنده : استخدام کار بازدید : 10 تاريخ : دوشنبه 27 شهريور 1396 ساعت: 1:53
    برچسب‌ها :