我是新手,并建立一个表单来创建一个新用户,但无论我在提交表单时出现什么模型错误:
System.Linq.Enumerable+d__16`2[System.Web.Mvc.ModelState,System.Web.Mvc.ModelError]
谁能看到我哪里出错?
查看
@model PGS.Models.ViewModels.CreateUserViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Main_Template.cshtml";
}
<h2>Create User</h2>
@using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, "User Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, "First Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, "Surname", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserGroup, "User Group", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.UserGroup, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserGroup, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(model => model.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(model => model.ConfirmPassword, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create User" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
模型
public class CreateUserViewModel
{
[Key]
public string Id { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Surname")]
public string LastName { get; set; }
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "User Group")]
public string UserGroup { get; set; }
[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; }
}
控制器
[ActionName("Create")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ApplicationDbContext DbContext, CreateUserViewModel model)
{
IEnumerable<ModelError> errors = ModelState.Values.SelectMany(v => v.Errors);
if (ModelState.IsValid)
{
IdentityResult ir;
var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(DbContext));
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext));
var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, UserGroup = model.UserGroup };
ir = um.Create(user, model.Password);
if (ir.Succeeded == false)
{
return View(model);
}
else
{
ir = um.AddToRole(user.Id, "pxpuser");
}
return RedirectToAction("Index", "Home");
}
Response.Write(errors);
return View(model);
}
答案 0 :(得分:0)
找到答案。
我在线设置断点:
if (ir.Succeeded == false)
它在Visual Studio中显示了我:
"User name ********* is invalid, can only contain letters or digits."