【问题标题】:Form can not be validated in .NET Core无法在 .NET Core 中验证表单
【发布时间】:2021-07-24 05:35:51
【问题描述】:
    <form id="formElem">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input asp-for="ID" type="hidden" />
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" required class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <textarea asp-for="Description" required rows="5" class="form-control"></textarea>
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
        </div>

        <div class="col-sm-6">
            <div class="form-group">
                <label asp-for="ImageData" class="control-label"></label>
                <input asp-for="ImageData" type="file" class="form-control" />
                <span asp-validation-for="ImageData" class="text-danger"></span>
            </div>
        </div>
    </div>




    <div class="form-group  col-sm-3">
        <input type="submit" id="dataSend" name="btn" value="Save" class="btn btn-primary" />
    </div>
</form>

JS:

        $("#dataSend").on('click', function (e) {
        e.preventDefault();

        var formData = new FormData();
        formData.append('ImageData', $('#ImageData')[0].files[0]);

        formData.append('ID', document.getElementById('ID').value);
        formData.append('Name', document.getElementById('Name').value);
        formData.append('Description', document.getElementById('Description').value);
        formData.append('btn', 'Save');

        $.ajax({
            contentType: false,
            processData: false,
            type: 'POST',
            url: '/Products/AddProduct',
            data: formData,
            success: function (response) {

                window.location.href = "/Products/Index";
            },
            error: function () {
                console.log("error.");
            },
        });
    });

当我点击保存按钮时,它直接调用AddProduct 操作,尽管所有表单字段都是空白的。我的问题是,尽管模型类中的 NameDescription 字段上有 [Required] 注释,为什么我的模型没有得到验证。当我使用 JavaScript 时会发生这种情况。

另外,我使用了jquery.validate.unobtrusive.min.jsjquery.validate.min.js 文件。

【问题讨论】:

    标签: .net asp.net-core unobtrusive-validation


    【解决方案1】:

    e.preventDefault(); 也会阻止表单验证。要在不提交表单的情况下检查验证,请使用 valid()。如果错误为$("#formElem").valid() is not a function.,请检查是否包含jquery.validate.min.js

    作为

    $("#dataSend").on('click', function (e) {
        e.preventDefault();
    
        $("#formElem").valid();
        ...
    

    在您的 AddProduct 方法中,您是否检查了模型状态?比如

    if (!ModelState.IsValid)
       {
         //Don't add product
         // return page();
      }
    

    【讨论】:

    • 感谢 Sayyed 验证工作,但在填写所有字段表单后未提交它给我错误 checkValidity is not property
    • @Abdulaziz 对于 jQuery,我们必须使用 valid() 方法。我已经更新了答案,如果您收到$("#formElem").valid() is not a function. 的错误,则包括jquery.validate.min.js
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多