我有一个登录和注册表单,当在常规View上使用时可以正常工作,但是,当我将该表单放入模式(Bootstrap v3)时,它无法发布数据(调试器显示所有字段为null提交时)。
@model myNamespace.Models.AllUserViewModels
<div class="container">
<div class="modal fade" id="LoginModal" tabindex="-1" role="dialog" keyboard="true" aria-labelledby="LoginModal" aria-hidden="true">
<div class="modal-dialog modal-sm" role="dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4>Login</h4>
</div>
<div class="modal-body">
<form asp-controller="User" asp-action="LoginSubmit" method="post" class="form-group">
<label asp-for="Log.Email">Email</label>
<input asp-for="Log.Email" placeholder="Email Address" class="form-control" id="Email">
<span asp-validation-for="Log.Email"></span>
<label asp-for="Log.Password">Password</label>
<input asp-for="Log.Password" placeholder="Password" class="form-control" id="Password">
<span asp-validation-for="Log.Password"></span>
<span>@ViewBag.loginError</span>
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!--End Login Modal -->
EDIT 我的表单没有发布到我的控制器的原因是我忘记在我的控制器方法中添加“POST”动词。
答案 0 :(得分:1)
根据documentation,asp-for
代码助手已经生成id
和name
属性。
为表达式名称生成id和name HTML属性 在asp-for属性中指定。
因此,您应该删除输入中的id属性。
<input asp-for="Log.Password" placeholder="Password" class="form-control">
答案 1 :(得分:1)
我找到了解决问题的方法。我的数据没有传递到我的后端的原因是因为在我的控制器中我忘了更改我用作方法的参数的视图模型的名称,以及在尝试时忘记更改对模型的引用做db调用,密码验证等等......
[HttpPost]
[Route("LoginSubmit")]
public IActionResult LoginSubmit(AllUserViewModels model)
{
if (ModelState.IsValid)
{
try
{
// If there are no errors upon form submit check db for proper creds.
User LoggedUser = _context.Users.SingleOrDefault(u => u.Email == model.Log.Email);
var Hasher = new PasswordHasher<User>();
// Check hashed password.
if (Hasher.VerifyHashedPassword(LoggedUser, LoggedUser.Password, model.Log.Password) != 0)
{
// Set user id in session for use in identification, future db calls, and for greeting the user.
HttpContext.Session.SetInt32("LoggedUserId", LoggedUser.Id);
return RedirectToAction("Account");
}
else
{
ViewBag.loginError = "Sorry, your password was incorrect.";
return View("landing");
}
}
// If no proper creds redirect to login page and return error.
catch
{
ViewBag.loginError = "Sorry, your email or password were incorrect.";
return View("landing");
}
}
// If form submit was illegal redirect to login and display model validation errors.
else
{
return View("landing");
}
}