'How could " possibly be the returned value for a new user
I have a form that I am trying to implement that creates a new user in the database. For some reason when it executes the controller function its returning " for the email address and returns me back to the view with a UserManager validation error. But I dont possibly see how it could be returning " when I can see its posting the email from the view.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using Rabbit.Application.Models;
using System.Threading.Tasks;
using Rabbit.Application.Identity.Models;
namespace Rabbit.Application.Identity.Controllers
{
public class AdminController : Controller
{
private UserManager<ApplicationUser> _userManager;
public AdminController(UserManager<ApplicationUser> usrMgr)
{
_userManager = usrMgr;
}
public IActionResult Index()
{
return View(_userManager.Users);
}
public ViewResult Create() => View();
[HttpPost]
public async Task<IActionResult> Create(User input)
{
if (ModelState.IsValid)
{
var appUser = new ApplicationUser
{
UserName = input.Name,
FirstName = input.FirstName,
LastName = input.LastName,
Email = input.Email,
};
var result = await _userManager.CreateAsync(appUser, input.Password);
if (result.Succeeded)
return RedirectToAction("Index");
else
{
foreach (var error in result.Errors)
//if (error.Code == "DuplicateUserName") continue;
ModelState.AddModelError(string.Empty, error.Description);
}
}
return View(input);
}
}
}
View
@using Rabbit.Application.Models
@model User
@section Scripts {
<partial name="_ValidationScriptsPartial"/>
}
@{
ViewData["Title"] = "Create";
}
<h1 class="bg-info text-white">Create User</h1>
<a asp-controller="Admin" asp-action="Index" class="btn btn-secondary">Back</a>
<div asp-validation-summary="All" class="text-danger"></div>
<form asp-controller="Admin" asp-action="Create" method="post">
<div class="form-group">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="ni ni-circle-08"></i>
</span>
</div>
<input asp-for="Email" type="text" class="form-control" placeholder="Email" autocomplete="email" />
</div>
<span asp-validation-for="Email" class="invalid-feedback" style="display: block"></span>
</div>
<div class="form-group">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="ni ni-circle-08"></i>
</span>
</div>
<input asp-for="Password" class="form-control" placeholder="Password" />
</div>
<span asp-validation-for="Password" class="invalid-feedback" style="display: block"></span>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
