字段验证问题返回类型错误

问题描述 投票:0回答:1

我在 pol 验证方面遇到问题,在下面的屏幕截图中我向您展示了我的问题。 如果方案小于零,它应该返回一个错误,但我不知道为什么它不起作用并返回这些错误 在此输入图片描述

enter image description here enter image description here

代码网站

@page
@using System.Web
@model WebApplication3.Pages.Declarations.EditableDeclarationLineModel
@{
    <link rel="stylesheet" href="/css/form.css" type="text/css" />
    <link rel="stylesheet" href="/css/details.css" type="text/css" />
    <link rel="stylesheet" href="/css/table.css" type="text/css" />
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Plus+Jakarta+Sans&display=swap">
}
@{
    ViewData["Title"] = "Edytuj wiersz deklaracji";
}

<script>

    }
    function getAmountFin(Amount) {
        var result = null;
        $.ajax({
            async: false,
            url: "?handler=ValidateDataVar2&Amount=" + Amount + "&VarNo=" + document.getElementById("VarietyNoSelect").value,
            success: function (data) {

                var SowingDate = new Date(document.getElementById('SowingDateInput').value);
                var SDm = SowingDate.getMonth() + 1;
                var SDd = SowingDate.getDay();
                const myArr = JSON.parse(data);
                const month = myArr[0];
                const day = myArr[1];
                if (month > SDm || isNaN(SDm)) {
                    var KGRate = document.getElementById('KGRate').value.replace(",", ".");
                    var AmountMat = document.getElementById('AmountOfMaterials').value.replace(",", ".");
                    var number = parseFloat(KGRate * AmountMat);
                    document.getElementById('Amount').value = number.toLocaleString("pl-PL");
                }
                else {
                    if (day >= SDd & month == SDm) {
                        var KGRate = document.getElementById('KGRate').value.replace(",", ".");
                        var AmountMat = document.getElementById('AmountOfMaterials').value.replace(",", ".");
                        var number = parseFloat(KGRate * AmountMat);
                        document.getElementById('Amount').value = number.toLocaleString("pl-PL");
                    }
                    else {
                        document.getElementById('Amount').value = 0;
                    }
                }
            }
        })
    }
    function PlantsVarietyList(GrainKind) {
        var result = null;
        $.ajax({
            async: false,
            url: "?handler=VarietyDropDownList&GrainKind=" + GrainKind + "&id1=" + document.getElementById("id1").value + "&TypeSowing=" + document.getElementById("TypeOfSowing").value,
            success: function (data1) {
               
                if (document.getElementById("GrainKind") != null) {
                    document.getElementById("VarietyNoSelect").removeAttribute("readonly");
                    document.getElementById("AreofLond").removeAttribute("readonly");

                }
                else {
                    document.getElementById("VarietyNoSelect").setAttribute("readonly", true);
                    document.getElementById("AreofLond").setAttribute("readonly", true);
                }
                removeOptions(document.getElementById("VarietyNoSelect"));
                var x = document.getElementById("VarietyNoSelect");
                var option = document.createElement("option");
                option.text = "-- Wybierz odmianę --";
                option.className = "left-align-option";
                x.add(option);

                data1.forEach((obj) => add1(obj));
                ;
            }
        })
    }
    function add1(obj) {
        var x = document.getElementById("VarietyNoSelect");
        var option = document.createElement("option");
        option.text = obj.text;
        option.setAttribute("value", obj.value);

        x.add(option);

    }
    function removeOptions(selectElement) {
        var i, L = selectElement.options.length - 1;
        for (i = L; i >= 0; i--) {
            selectElement.remove(i);
        }
    }
    function makeFieldEditable(TypeOfSowing) {
        var result = null;
        $.ajax({
            async: false,
            url: "?handler=ValidateDataVar3&TypeOfSowing=" + document.getElementById("TypeOfSowing").value + "&id1=" + document.getElementById("id1").value,
            success: function (data1) {
                removeOptions(document.getElementById("GrainKind"));
                var x = document.getElementById("GrainKind");
                var option = document.createElement("option");
                option.text = "-- Wybierz gatunek odmiany --";
                option.className = "left-align-option";
                x.add(option);

                data1.forEach((obj) => add2(obj));

                removeOptions(document.getElementById("VarietyNoSelect"));
                var x = document.getElementById("VarietyNoSelect");
                var option = document.createElement("option");
             
                document.getElementById("VarietyNoSelect").
                option.text = "-- Wybierz odmianę --";
                option.className = "left-align-option";
                x.add(option);

                data1.forEach((obj) => add1(obj));
                document.getElementById("VarietyNoSelect").readOnly = true;
            }
        })
    }
    function add2(obj) {
        var x = document.getElementById("GrainKind");
        var option = document.createElement("option");
        option.text = obj.text;
        option.setAttribute("value", obj.value);

        x.add(option);

    }
    function makeFieldEditable2(SowingDate) {
        if (SowingDate != null) {
            document.getElementById("AmountOfMaterials").removeAttribute("readonly");
        }
    }
</script>
<style>
    .left-align-option {
        text-align: left;
    }
</style>
<div class="head">
    <a asp-page="./DeclarationsList"><img style="transform: rotate(90deg);" src="~/image/strzalka2.png" /></a>
    <div class="Title-Home">Strona główna- Szczegóły deklaracji- Edytuj siew </div>
    <div>
        <b class="Title">Edytuj siew</b>
    </div>
</div>
<p>@Model.DocNo</p>
<form class="FormCreateDecLine" method="post">
    <div id="general-data">
        <div class="form-information">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-groupAddSowing">
                <label class="control-label" >Rodzaj siewu</label>
                <select asp-for="AgnasLine.TypeofSowing" class="FormSelectDeclar" asp-items="@Model.TypeOfSowing"  id="TypeOfSowing" onchange="makeFieldEditable(this.value)" >
                    <option value=""> Wybierz rodzaj siewu </option>
                </select>
                <span asp-validation-for="AgnasLine.TypeofSowing" class="text-danger"></span>
            </div>
            <div class="form-groupAddSowing" hidden=true>
                <label class="control-label">Nr odmiany</label>
                <input asp-for="AgnasLine.Variety" class="FormControlDeclar" />
                <span asp-validation-for="AgnasLine.Variety" class="text-danger"></span>

            </div>
            <div class="form-groupAddSowing">
                <label class="control-label">Gatunek</label>
                <select asp-for="AgnasLine.GrainKind" class="FormSelectDeclar" data-val="false" id="GrainKind" asp-items="@Model.GrainKind" onchange="PlantsVarietyList(this.value)" autofocus required title="To pole jest wymagane.">
                    <option value="">-- Wybierz rodzaj ziarna --</option>
                </select>
                <span asp-validation-for="AgnasLine.GrainKind" class="text-danger"></span>
            </div>
            <div class="form-groupAddSowing">
                <label class="control-label">Nazwa odmiany</label>
               
                @* <select asp-for="AgnasLine.VarietyNo" class="FormControlDeclar" asp-items="@Model.VarietyName" id="VarietyNoSelect" onchange="getAmount(this.value)">
                <option value="">-- Wybierz odmianę --</option>
                </select>*@
                <select asp-for="AgnasLine.VarietyNo" class="FormSelectDeclar" id="VarietyNoSelect" data-val="false" asp-items="@Model.VarietyName" onchange="getAmount(this.value)" readonly autofocus required title="To pole jest wymagane.">
                    <option>Nazwa odmiany</option>
                </select>
                <span asp-validation-for="AgnasLine.VarietyNo" class="text-danger"></span>
            </div>
            <div class="form-groupAddSowing" id="SowingDate" hidden>
                <label class="control-label" >Data siewu</label>
                <input asp-for="AgnasLine.SowingDate" class="FormControlDeclar" id="SowingDateInput" onchange="makeFieldEditable2(this.value)" placeholder="Data siewu" />
                <span asp-validation-for="AgnasLine.SowingDate" class="text-danger"></span>
            </div>
            <div class="form-groupAddSowing">
                <label class="control-label">Ilość materiału ze zbioru kg</label>
                <input asp-for="AgnasLine.AmountOfMaterials"  class="FormControlDeclar" formnovalidate id="AmountOfMaterials" placeholder="Ilość materiału ze zbioru kg" min="0,01" data-val="true" data-val-range="Wartość musi być większa niż 0." data-val-required="To pole jest wymagane." onchange="getAmountFin(this.value)" autofocus required title="To pole jest wymagane." />
                <span asp-validation-for="AgnasLine.AmountOfMaterials" class="text-danger"></span>
            </div>
            <div class="form-groupAddSowing">
                <label class="control-label">Powierzchnia obsiana ha</label>
                <input asp-for="AgnasLine.TheAreaOfLandPlanted" class="FormControlDeclar" data-val="false" id="AreofLond" placeholder="Powierzchnia obsiana ha"  autofocus required title="To pole jest wymagane." />
                <span asp-validation-for="AgnasLine.TheAreaOfLandPlanted" class="text-danger"></span>
            </div>

该项目的假设是,当输入零或小于零的值时,它应该返回错误,因此在代码中它使用 data-val ,错误是当我有 2 位小数时,如果我有 3 位系统让我通过,我不知道为什么 而如果我将此值添加到 AgnasLine 类

[Required(ErrorMessage = "This field is required.")]

[Range(0.01, double.MaxValue, ErrorMessage = "The value must be greater than 0.")]
那么我将无法接收来自另一个平台的文档,该文档将数据发送到值为 0 的页面

c# .net asp.net-core razor-pages
1个回答
0
投票

为了解决这个问题,我使用了 aria-invalid 和 javascript,这使得验证能够正常工作

javascript

function CheckValidation(){
    var AmountofMatertial = document.getElementById('AmountOfMaterials');
    var AmountofMat = document.getElementById('AmountOfMaterials').value.replace(",", ".");
    var TheAreaOfPlantes = document.getElementById('AreofLond');
    var TheAreOfPlan = document.getElementById('AreofLond').value.replace(",", ".");
    visible=true;
    if (AmountofMat <= 0) {
        AmountofMatertial.setAttribute('aria-invalid', true);
        visible=false;
    } 
    else 
    {
        AmountofMatertial.setAttribute('aria-invalid', false);
    }
    if (TheAreOfPlan <= 0) { 
        TheAreaOfPlantes.setAttribute('aria-invalid', true);
        visible=false;
    }
    else{
        
        TheAreaOfPlantes.setAttribute('aria-invalid', false);
    }
    return visible;
}

html

    <input asp-for="AgnasLine.AmountOfMaterials" class="FormControlDeclar" formnovalidate id="AmountOfMaterials" aria-invalid="false" aria-errormessage="err1" placeholder="Ilość materiału ze zbioru kg" data-val="false" onchange="getAmountFin(this.value)" autofocus required title="To pole jest wymagane." />
     <span id="err1" class="errormessage">Wartość musi być większa niż 0.</span>
               

© www.soinside.com 2019 - 2024. All rights reserved.