因此,即使我明确要求它具有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);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind("CommentaarText, Tijdstip")] int id, IFormCollection collection) //Bind = protect from overposting
    {
        try
        {
            // Creating  object to POST
            Commentaar commentaar = new Commentaar
            {
                UserId = _userManager.GetUserId(HttpContext.User),
                StadId = id,
                CommentaarText = collection["Commentaar"],
                Tijdstip = DateTime.Now
            };

            var result = _dataGentService.PostCommentaar(commentaar);

            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }


有什么我想念的吗?我认为除数据注释之外的所有工作都是由MVC完成的吗?

最佳答案

您的输入是:

<input asp-for="Commentaar" class="form-control" />


您必须将asp-for从Commentaar更改为Commentaar.CommentaarText,以便对其进行验证:

<div class="form-group">
    <label asp-for="Commentaar.CommentaarText" class="control-label"></label>
    <input asp-for="Commentaar.CommentaarText" class="form-control" />
    <span asp-validation-for="Commentaar.CommentaarText" class="text-danger"></span>
</div>


更新:

在将其传递给视图之前,在您的视图模型中初始化Commentaar对象:

public ActionResult Create(int id)
{
    CommentaarCreate_VM vm = new CommentaarCreate_VM()
    {
        Stad = _dataGentService.GetStadFromId(id),
        Commentaar = new Commentaar()
    };

    return View(vm);
}

10-05 19:15