I have a problem with register user in my MVC 4 application.
I have created a CMS with Identity framework, and if I make a user directly in the database the login work fine.
The user is signed in as "Admin" and it can access my Register page for creating a new user.
The problem is when i try to submit the register form it says something like :
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
I have tried to debug and it seems like its getting all the input correctly.
Im pretty new to all this stuff, so im sorry if my code is messy.
Hope you pro people can help a newbie :)
My code looks like this:
// POST: /Account/Register
[HttpPost]
[Authorize(Roles = "Admin")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
This is my model for Account
> using Jagtside.Models; using
> Microsoft.AspNet.Identity.EntityFramework; using
> System.Collections.Generic; using
> System.ComponentModel.DataAnnotations;
namespace Jagtside.Models
{
public class ManageUserViewModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Gamle password")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Nyt password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "De to passwords matcher ikke hinanden, prøv igen")]
public string ConfirmPassword { get; set; }
}
public class LoginViewModel
{
[Required]
[Display(Name = "Brugernavn")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Husk mig?")]
public bool RememberMe { get; set; }
}
public class RegisterViewModel
{
[Required]
[Display(Name = "Brugernavn")]
public string UserName { 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 = "Bekræft password")]
[Compare("Password", ErrorMessage = "De to passwords matcher ikke hinanden, prøv igen")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// New Fields added to extend Application User class:
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
// Return a pre-poulated instance of AppliationUser:
public ApplicationUser GetUser()
{
var user = new ApplicationUser()
{
UserName = this.UserName,
FirstName = this.FirstName,
LastName = this.LastName,
Email = this.Email,
};
return user;
}
}
public class EditUserViewModel
{
public EditUserViewModel() { }
// Allow Initialization with an instance of ApplicationUser:
public EditUserViewModel(ApplicationUser user)
{
this.UserName = user.UserName;
this.FirstName = user.FirstName;
this.LastName = user.LastName;
this.Email = user.Email;
}
[Required]
[Display(Name = "Brugernavn")]
public string UserName { get; set; }
[Required]
[Display(Name = "Navn")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Efternavn")]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
}
public class SelectUserRolesViewModel
{
public SelectUserRolesViewModel()
{
this.Roles = new List<SelectRoleEditorViewModel>();
}
// Enable initialization with an instance of ApplicationUser:
public SelectUserRolesViewModel(ApplicationUser user) : this()
{
this.UserName = user.UserName;
this.FirstName = user.FirstName;
this.LastName = user.LastName;
var Db = new ApplicationDbContext();
// Add all available roles to the list of EditorViewModels:
var allRoles = Db.Roles;
foreach(var role in allRoles)
{
// An EditorViewModel will be used by Editor Template:
var rvm = new SelectRoleEditorViewModel(role);
this.Roles.Add(rvm);
}
// Set the Selected property to true for those roles for
// which the current user is a member:
foreach(var userRole in user.Roles)
{
var checkUserRole = this.Roles.Find(r => r.RoleName == userRole.Role.Name);
checkUserRole.Selected = true;
}
}
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<SelectRoleEditorViewModel> Roles { get; set; }
}
// Used to display a single role with a checkbox, within a list structure:
public class SelectRoleEditorViewModel
{
public SelectRoleEditorViewModel() {}
public SelectRoleEditorViewModel(IdentityRole role)
{
this.RoleName = role.Name;
}
public bool Selected { get; set; }
[Required]
public string RoleName { get; set;}
}
}
Then my view for register
@model Jagtside.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
<section class="section-container">
<section id="loginForm">
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h2 class="heading">Opret ny bruger</h2>
@Html.ValidationSummary()
<section id="login-form-container">
<!-- Brugernavn -->
<section class="input-box">
@*@Html.LabelFor(m => m.UserName, new { @class = "" })*@
@Html.TextBoxFor(m => m.UserName, new { @class = "form-input", @placeholder = "Brugernavn" })
@*<input type="text" name="username" id="username" class="form-input" placeholder="Brugernavn" required /><br />*@
</section>
<!-- Password -->
<section class="input-box">
@*@Html.LabelFor(m => m.Password, new { @class = "" })*@
@Html.PasswordFor(m => m.Password, new { @class = "form-input", @placeholder = "Password" })
@*<input type="password" name="password" id="password" class="form-input" placeholder="Password" required /><br />*@
</section>
<!-- Confirm Password -->
<section class="input-box">
@*@Html.LabelFor(m => m.ConfirmPassword, new { @class = "" })*@
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control", @placeholder = "Bekræft Password" })
@*<input type="password" name="ConfirmPassword" id="ConfirmPassword" class="form-input" placeholder="Bekræft password" required /><br />*@
</section>
<!-- Email -->
<section class="input-box">
@*@Html.LabelFor(m => m.Email, new { @class = "" })*@
@Html.TextBoxFor(m => m.Email, new { @class = "form-input", @placeholder="Email" })
@*<input type="text" name="email" id="email" class="form-input" placeholder="Email" required /><br />*@
</section>
<!-- Firstname -->
<section class="input-box">
@*@Html.LabelFor(m => m.FirstName)*@
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-input", @placeholder="Fornavn" })
</section>
<!-- Lastname -->
<section class="input-box">
@*@Html.LabelFor(m => m.LastName)*@
@Html.TextBoxFor(m => m.LastName, new { @class = "form-input", @placeholder = "Efternavn" })
</section>
<input type="submit" class="submit-btn" value="Opret" />
</section>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
</section>