MVC Razor Html 表单验证未捕获错误

MVC Razor Html form validation not catching errors

我正在尝试制作一个简单的表格,它接受两个必需的输入和一个可选的输入(还有一个 属性 即 'hidden' - TaskId - 并设置loading,设置后不改变)。

提交时出现问题。验证被完全跳过,不管我在框中放什么,它总是直接进入方法并且不向用户显示任何验证文本。此外,无论如何,ModelState 始终有效。


更新

张贴文字而不是图片。对不起那些家伙。

查看

@model ScrumBoard.Models.ViewModels.UpdateTaskViewModel
@{
    HtmlHelper.ClientValidationEnabled = true;
}

@using (Html.BeginForm("EditTask", "Dashboard", FormMethod.Post, new {    @class = "px-4 py-3" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    @Html.HiddenFor(o => o.TaskId)

    <div class="form-group">
        @Html.LabelFor(o => o.Title)
        @Html.TextBoxFor(o => o.Title, new { @class = "form-control" })
        @Html.ValidationMessageFor(o => o.Title)
    </div>
    <div class="form-group">
        @Html.LabelFor(o => o.Description)
        @Html.TextAreaFor(o => o.Description, new { @class = "form-control", rows = 3 })
        @Html.ValidationMessageFor(o => o.Description)
    </div>
    <div class="form-group">
        @Html.LabelFor(o => o.Comment)
        @Html.TextAreaFor(o => o.Comment, new { @class = "form-control", rows = 2, maxlength = 100 })
        @Html.ValidationMessageFor(o => o.Description)
    </div>
    <button type="submit" class="btn btn-primary">Update</button>
}

ViewModel

public class UpdateTaskViewModel
{
    public UpdateTaskViewModel(int taskId)
    {
        TaskId = taskId;
    }

    public int TaskId { get; set; }

    [Required(ErrorMessage = "Title is required", AllowEmptyStrings = false)]
    [AllowHtml]
    public string Title { get; set; }

    [Required(ErrorMessage = "Description is required", AllowEmptyStrings = false)]
    [AllowHtml]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [AllowHtml]
    [DataType(DataType.MultilineText)]
    public string Comment { get; set; }
}

控制器

    [HttpPost]
    public ActionResult EditTask(int taskId, string title, string description, string comment = "")
    {
        Alert alert;
        if (ModelState.IsValid)
        {
            try
            {
                DatabaseOperations.UpdateTask(
                    taskId,
                    title,
                    description,
                    EacId,
                    comment);
                alert = new Alert("Success!", "Updated task.", "alert-success");
            }
            catch (Exception e)
            {
                alert = new Alert("Error!", "Failed to update task.", "alert-danger", e);
            }
        }
        else
        {
            alert = new Alert("Warning!", "ModelState is invalid.", "alert-warning");
        }

        TempData["Alert"] = alert;
        return RedirectToAction("Index");
    }

如此简单的答案...所有需要做的就是不要将每个参数单独传递给控制器​​方法,只需传递 ViewModel,一切都会按预期进行:

[HttpPost]
public ActionResult EditTask(UpdateTaskViewModel model)
{
    Alert alert;
    if (ModelState.IsValid)
    {
        try
        {
            DatabaseOperations.UpdateTask(
                model.TaskId,
                model.Title,
                model.Description,
                EacId,
                model.Comment);
            alert = new Alert("Success!", "Updated task.", "alert-success");
        }
        catch (Exception e)
        {
            alert = new Alert("Error!", "Failed to update task.", "alert-danger", e);
        }
    }
    else
    {
        return PartialView("_UpdateTask")
    }

    TempData["Alert"] = alert;
    return RedirectToAction("Index");
}

PS。因为这是我用来生成表单的局部视图,所以我需要将带有错误模型的局部视图发送回我刚刚在 @using(Html.BeginForm(...)) 中设置的主视图以替换 html 局部视图。