我的MVC 4应用程序中的注册用户有问题。
我已经使用Identity框架创建了一个CMS,如果我直接在数据库中创建用户,则登录工作正常。
用户以“管理员”身份登录,并且可以访问我的“注册”页面以创建新用户。
问题是当我尝试提交注册表时,它说的是:
一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。
我试过调试,似乎正确地获取所有输入。
我对所有这些东西都很陌生,所以我很抱歉,如果我的代码很乱。
希望你们亲们可以帮助新手:)
我的代码如下所示:
// 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);
}
这是我的帐户模型
> 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;}
}
}
然后我的注册视图
@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>