所以,当为我的模型输入文本时,它总是有效的,即使我明确要求它有一个 minLength,尽管它是空的或小于 minLength。
楷模:
public class CommentaarCreate_VM
{
public Stad Stad { get; set; }
[Required]
public Commentaar Commentaar { get; set; }
}
public class Commentaar
{
[Key]
public int CommentaarId { get; set; }
[Required]
public string UserId { get; set; }
[Required]
public int StadId { get; set; }
[Required(AllowEmptyStrings=false, ErrorMessage="You need to enter a comment of valid length")]
[MinLength(5, ErrorMessage ="You need to enter a comment of valid length")]
public string CommentaarText { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime Tijdstip { get; set; }
}
看法:
@model DataGent.Web.ViewModels.CommentaarCreate_VM
@{
ViewData["Title"] = "Create new comment";
}
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
<input type="hidden" asp-for="Stad.Id" />
<input type="hidden" asp-for="Stad.Naam" />
<input type="hidden" value="@Html.AntiForgeryToken()" />
<div class="form-group">
<label asp-for="Commentaar" class="control-label"></label>
<input asp-for="Commentaar" class="form-control" />
<span asp-validation-for="Commentaar.CommentaarText" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</form>
</div>
控制器动作:
public ActionResult Create(int id)
{
CommentaarCreate_VM vm = new CommentaarCreate_VM()
{
Stad = _dataGentService.GetStadFromId(id),
Commentaar = null
};
return View(vm);
}
有什么我想念的吗?我以为除了数据注释之外的所有工作都是由 MVC 完成的?
鸿蒙传说
RISEBY
素胚勾勒不出你
相关分类