" 怎么可能是新用户的返回值
How could " possibly be the returned value for a new user
我有一个正在尝试实现的表单,它在数据库中创建了一个新用户。出于某种原因,当它执行控制器函数时,它会返回“作为电子邮件地址,returns 我会返回到带有 UserManager 验证错误的视图。但是当我看到它的发布时,我可能看不到它是如何返回的来自视图的电子邮件。
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);
}
}
}
查看
@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>
不确定是什么导致输入 " 但我通过控制器重新编码了路由并且一切开始正常工作。我相信它正在尝试加载 post 方法并绕过对 [= 的初始调用10=]
这可能是因为它是新手,没有完全理解路由,并且收到了可能很难追踪的虚假结果。一旦我退后一步,从路由方面详细了解控制器和模型如何与视图一起工作,我就能够让我所做的一切正常工作。
我有一个正在尝试实现的表单,它在数据库中创建了一个新用户。出于某种原因,当它执行控制器函数时,它会返回“作为电子邮件地址,returns 我会返回到带有 UserManager 验证错误的视图。但是当我看到它的发布时,我可能看不到它是如何返回的来自视图的电子邮件。
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);
}
}
}
查看
@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>
不确定是什么导致输入 " 但我通过控制器重新编码了路由并且一切开始正常工作。我相信它正在尝试加载 post 方法并绕过对 [= 的初始调用10=] 这可能是因为它是新手,没有完全理解路由,并且收到了可能很难追踪的虚假结果。一旦我退后一步,从路由方面详细了解控制器和模型如何与视图一起工作,我就能够让我所做的一切正常工作。