我的Web应用程序是ASP.NET MVC5,我正在使用VS Express 2013.2 for Web。我的问题是注册一个新帐户。出于某种原因,从AccountController.cs返回一个错误,指出' Name不能为null或为空'即使它在我追踪它时包含了这个名字。
IdentityModels.cs:
public class ApplicationUser : IdentityUser
{
//public string Email { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string Phone { get; set; }
public string CountryCode { get; set; }
public string StateProvince { get; set; }
public string StateProvinceCode { get; set; }
public string Language { get; set; }
}
RequestAccount.cshtml:
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
</div>
AccountViewModels.cs:
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
AccountController.cs
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> RequestAccount(RequestAccountViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() {
//UserName = model.UserName,
Email = model.Email,
Name = model.Name,
Company = model.Company,
Phone = model.Phone,
CountryCode = model.CountryCode,
StateProvince = model.StateProvince,
StateProvinceCode = model.StateProvinceCode,
Language = model.Language };
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);
}
当我追踪RequestAccount时,我可以看到model.Name包含&#34; John Doe&#34;,但result.Succeeded
总是返回false
,错误为Name cannot be null or empty
。我不明白为什么当我清楚地看到名字不是......时,名称为空的错误...
有人知道我做错了什么,或者我如何进一步测试以确定问题?
我已经在SO上看到了帖子中包含的屏幕截图,我在调试中创建了一个本地人,但我不知道如何在我的问题中包含它。当然,model.Name = "John Doe"
和user.Name = "John Doe"
。 :S
答案 0 :(得分:1)
您需要在ApplicationUser中设置用户名。
正如您的规范声明他们应该使用他们的电子邮件登录集
Username = model.Email