[英]How do I validate forms properly in asp.NET MVC?
I'm trying to validate my form but it just wont show up or be enforced for some reason?我正在尝试验证我的表单,但由于某种原因它不会显示或强制执行?
I fill in the info and it will give me the info if I fill in the stuff so I know my post goes through.我填写信息,如果我填写这些信息,它会给我信息,所以我知道我的帖子通过了。 it just wont enforce any requirements.它只是不会强制执行任何要求。 Viewmodel tied to the form: Viewmodel 绑定到表单:
using System.ComponentModel.DataAnnotations;
namespace PastaShop.Models
{
public class PersoonViewModel:IValidatableObject
{
[Display(Name ="Naam")]
[Required(ErrorMessage = "je moet een naam ingeven !")]
public string? Naam { get; set; }
[Display(Name = "Voornaam")]
[Required(ErrorMessage = "je moet een voornaam ingeven !")]
public string? Voornaam { get; set; }
[Display(Name = "Email")]
[Required(ErrorMessage ="je moet een Email ingeven !")]
[EmailAddress]
public string? Email { get; set; }
[Display(Name = "Telefoon")]
[Required(ErrorMessage = "je moet een Email ingeven !")]
public string? Telefoon { get; set; }
[Display(Name = "Geboortedatum")]
[Required(ErrorMessage = "je moet een datum ingeven !")]
public DateTime Geboortedatum { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var validationResults = new List<ValidationResult>();
if (DateTime.Compare(Geboortedatum, DateTime.Now) > 0)
{
validationResults.Add(new ValidationResult("GeboorteDatum is in de toekomst !"));
}
return validationResults;
}
}
}
cshtml page tied to the form: cshtml页面绑定到表单:
@{
ViewData["Title"] = "Nieuwsbrief";
}
<h1>Nieuwsbrief</h1>
@model PersoonViewModel
@{
ViewBag.Title = "Nieuwsbrief";
}
<div id="niewsbriefForm">
<form asp-action="bevestigNieuwsBrief" method="post">
<label asp-for="Naam"></label>
<input type="text" asp-for="Naam" />
<span asp-validation-for="Naam"></span>
<br />
<label asp-for="Voornaam"></label>
<input type="text" asp-for="Voornaam" />
<span asp-validation-for="Voornaam"></span>
<label asp-for="Email"></label>
<input type="email" asp-for="Email" />
<span asp-validation-for="Email"></span>
<label asp-for="Telefoon"></label>
<input type="text" asp-for="Telefoon" />
<span asp-validation-for="Telefoon"></span>
<label asp-for="Geboortedatum"></label>
<input type="text" asp-for="Geboortedatum" />
<span asp-validation-for="Geboortedatum"></span>
<input type="submit" value="Bevestigen" class="btn btn-primary" />
<div asp-validation-summary="All">Er zijn Validatiefouten! </div>
</form>
</div>
actions in the homecontroller:家庭控制器中的操作:
public IActionResult Nieuwsbrief()
{
var nieuwsbriefViewModel = new PersoonViewModel();
return View(nieuwsbriefViewModel);
}
[HttpPost]
public IActionResult BevestigNieuwsBrief(PersoonViewModel persoonViewModel)
{
if (this.ModelState.IsValid)
{
var persoon = new Persoon();
persoon.Email = persoonViewModel.Email;
persoon.Naam = persoonViewModel.Voornaam;
return View(persoon);
} else
{
return RedirectToAction(nameof(Nieuwsbrief));
}
}
Any help would be greatly appreciated !任何帮助将不胜感激 !
It could be that the validation JavaScript is not being loaded.可能是没有加载验证 JavaScript。 Check that your "_Layout.cshtml" has a line like this:检查您的“_Layout.cshtml”是否有这样的一行:
@await Html.PartialAsync("_ValidationScriptsPartial")
For a project of mine, this is just above the @RenderSection("Scripts, false)
line and below some other scripts, such as jquery.min.js, site.js, etc.对于我的一个项目,它位于@RenderSection("Scripts, false)
行的上方和其他一些脚本的下方,例如 jquery.min.js、site.js 等。
Also check that the "_ValidationScriptsPartial.cshtml" file has lines like this:还要检查“_ValidationScriptsPartial.cshtml”文件是否有这样的行:
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
place this under your view @Html.ValidationSummary(false)把它放在你的视图下@Html.ValidationSummary(false)
<input type="text" asp-for="Naam" />
also add required for the validate item还需要为验证项目添加
<input type="text" asp-for="Naam" required/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.