其中一个Form post字段返回NULL.NET Core MVC



我有一个表单和一些字段要发布。当模型被发送到控制器方法时,除了始终为NULL的电子邮件地址字段外,所有参数都可以。我不明白自己做错了什么。

模型的屏幕截图在这里

控制器和方法的屏幕截图+空字段在这里

我提交的表格在这里

项目文件在我的Repo 中

注意,除了电子邮件地址密码确认之外,所有字段都可以!

[Key]
public int Id { get; set; }
[Required]
[Display(Name = "First Name")]
[MaxLength(20, ErrorMessage = "Firstname cannot be contain more tahn 20 characters")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[MaxLength(20, ErrorMessage = "Lastname cannot be contain more tahn 20 characters")]        
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email Address")]
[MaxLength(40, ErrorMessage = "Email Address cannot contain more that 40 characters")]        
public string EmailAddress { get; set; }
[Required]
[Display(Name = "Password")]
[MinLength(8, ErrorMessage = "Password must be atleast 8 characters")]
[MaxLength(16, ErrorMessage = "Password cannot contain more that 16 characters")]        
public string Password { get; set; }
[Required]
[Display(Name = "Password Confirmations")]
[MinLength(8, ErrorMessage = "Password must be atleast 8 characters")]
[MaxLength(16, ErrorMessage = "Password cannot contain more that 16 characters")]
public string PasswordConfirmation { get; set; }

注册控制器:

private readonly UserContext dbContext;
public SignupController(UserContext _dbContext)
{
this.dbContext = _dbContext;
}
public IActionResult Index()
{
return View();
}
//[HttpPost]
//[ValidateAntiForgeryToken]
public IActionResult CreateUser(User user)
{
// Check model validation
if (!ModelState.IsValid)
ViewBag.message = "AAAAA";
// Check duplication
if (dbContext.users.Any(e => e.EmailAddress == user.EmailAddress))
return ViewBag("Email address registered");

dbContext.Add(user);
dbContext.SaveChanges();
ViewBag.message = "The user " + user.FirstName + " " + user.LastName + " is saved successfully";
//return View();
return Content("Hi");
@model DataAccessLibrary.Models.User;
@{
Layout = "_Layout";
ViewBag.Title = "Sign up";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Login Page - Product Admin Template</title>
</head>
<body>
<div class="container tm-mt-big tm-mb-big">
<div class="row">
<div class="col-12 mx-auto tm-login-col">
<div class="tm-bg-primary-dark tm-block tm-block-h-auto">
<div class="row">
<div class="col-12 text-center">
<h2 class="tm-block-title mb-4">Create your account</h2>
</div>
</div>
<div class="row mt-2">
<div class="col-12">
<form id="signup_form" name="signup_form" asp-action="CreateUser" asp-controller="Signup" method="post" class="tm-login-form">
<!-- Firstname - Start -->
<div class="form-group inline-input-container">
<label for="firstname">First Name</label>
<input name="firstname" type="text" class="form-control validate" maxlength="20" id="firstname" value="" asp-for="FirstName" required />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<!-- Firstname - End -->
<!-- Lastname - Start -->
<div class="form-group inline-input-container">
<label for="lastname">Last Name</label>
<input name="lastname" type="text" class="form-control validate" maxlength="20" id="lastname" value="" asp-for="LastName" required />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<!-- Lastname - End -->
<!-- Email - Start -->
<div class="form-group">
<label for="email">Email Address</label>
<input name="email" type="text" class="form-control validate" id="email" maxlength="40" value="" asp-for="EmailAddress" required />
<span asp-validation-for="EmailAddress" class="text-danger"></span>
</div>
<!-- Email - End -->
<!-- Password - Start -->
<div class="form-group mt-3 inline-input-container">
<label for="password">Password</label>
<input name="password" type="password" class="form-control validate" maxlength="16" id="password" value="" asp-for="Password" required />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<!-- Password - End -->
<!-- Confirm Password - Start -->
<div class="form-group mt-3 inline-input-container">
<label for="confirm_password">Confirm Password</label>
<input name="confirm_password" type="password" maxlength="16" class="form-control validate" id="confirm_password" asp-for="PasswordConfirmation" value="" required />
</div>
<!-- Confirm Password - End -->
<div class="form-group mt-4">
<button id="submission" type="submit" class="btn btn-primary btn-block text-uppercase">Sign up</button>
</div>
<b id="msg"></b>
</form>
<div class="form-group mt-4">
<a asp-action="Index" asp-controller="Home" class="btn btn-primary btn-block text-uppercase inline" style="width: 60%">Resend Confirmation?</a>
<a asp-controller="Home" asp-action="Login" class="btn btn-primary btn-block text-uppercase inline" style="width: 40%">Login</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
@section Scripts{
<script type="text/javascript">        
//$('#submission').click(function () {
//    var form = $("#signup_form");
//    var url = form.attr("action");
//    var formData = form.serialize();
//    $.post(url, formData, function (data) {
//        $("#msg").html(data);
//    });
//})
</script>
}

您正在使用"id"one_answers"name"元素覆盖属性的名称。因此,没有使用"asp-for"来正确呈现属性。用于绑定工作:

<input type="text" class="form-control validate" maxlength="40" value="" asp-for="EmailAddress" required />

玩得开心!

最新更新