You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC 5中INT类型EditorFor字段致表单验证绕过求助

ASP.NET MVC 5 表单验证被INT字段绕过问题求助

在ASP.NET MVC 5项目中遇到以下问题:使用EditorFor()渲染的INT类型quantity字段,虽然不是服务端必填项,但它的存在会导致表单验证被绕过,表单直接提交。临时移除该字段时,表单验证可正常工作;重新添加后,验证又会失效。

已排查许久,经大量调研仍未找到解决方案,特附上相关代码求助:

模型代码

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyProject.Models
{
    public class OrderViewModel
    {
        public OrderViewModel()
        {
            // Initialize orderItems with an empty list in the constructor
            orderItems = new List<OrderItemViewModel>();
            orderItems.Add(new OrderItemViewModel()); // Add an initial item
        }

        public int masterQuoteId { get; set; }
        public string quotationNumber { get; set; }

        [Required(ErrorMessage = "Select one.")]
        public int customerId { get; set; }

        public string customerName { get; set; }
        public string cityName { get; set; }

        [Required(ErrorMessage = "Select one.")]
        public int incoTermId { get; set; }

        public string incoTerm { get; set; }
        [Required(ErrorMessage = "Select one.")]
        public int paymentTermId { get; set; }

        public string paymentTerm { get; set; }
        public string createdDate { get; set; }

        // Master Quotation Details Fields
        public List<OrderItemViewModel> orderItems { get; set; }
        public List<QuotItems> orderItemsJson { get; set; }
    }

    public class OrderItemViewModel
    {
        public int quoteItemId { get; set; }
        public int masterQuoteId { get; set; }
        public string quotationNumber { get; set; }
        public int supplierId { get; set; }
        public string supplierName { get; set; }
        public int productCatId { get; set; }
        public string productCat { get; set; }
        public int product_Cat_Model_Id { get; set; }
        public string productCat_Model { get; set; }
        public string description { get; set; }
        public decimal unitPrice { get; set; }
        public string unitPriceDisplay { get; set; }
        public int quantity { get; set; }
        public decimal discountInPercent { get; set; }
        public decimal discountInAmount { get; set; }
        public string discountInAmountDisplay { get; set; }
        public decimal totalAmount { get; set; }
        public string totalAmountDisplay { get; set; }
        public string currenceySymbol { get; set; }
        public decimal discountedUnitPrice { get; set; }
        public string discountedUnitPriceDisplay { get; set; }
     }
}

PartialView代码

<table class="tableTable table-bordered" id="orderDetails" width="100%" cellspacing="0">
    <tbody>
        @for (int i = 0; i < Model.orderItems.Count; i++)
        {
            <tr class="mainRow">
                <td>
                    @Html.DropDownListFor(model => model.orderItems[i].supplierId, ViewData["SupplierNameList_" + i] as SelectList, "-- Select Supplier --", htmlAttributes: new { @class = "form-control supplier", id = "supplierDropdown" + i.ToString() })
                </td>
                <td>
                    @Html.DropDownListFor(model => model.orderItems[i].productCatId, ViewData["ProductCatList_" + i] as SelectList, "-- Select Cat --", htmlAttributes: new { @class = "form-control productCat", id = "productCatDropdown" + i.ToString() })
                </td>
                <td>
                    @Html.DropDownListFor(model => model.orderItems[i].product_Cat_Model_Id, ViewData["ProductModelList_" + i] as SelectList, "-- Select Cat --", htmlAttributes: new { @class = "form-control productModel", id = "productCatDropdown" + i.ToString() })
                </td>
                <td>
                    @Html.EditorFor(model => model.orderItems[i].unitPrice, new { htmlAttributes = new { @class = "form-control qPrice", @autocomplete = "off", @placeholder = "Unit Price", id = "unitPrice" + i.ToString(), @data_val = "false" } })
                </td>
                <td>
                    @Html.EditorFor(model => model.orderItems[i].quantity, new { htmlAttributes = new { @class = "form-control quantity", @autocomplete = "off", @placeholder = "Qty", id = "quantity" + i.ToString(), @data_val = "false" } })
                </td>
                <td>
                    @Html.EditorFor(model => model.orderItems[i].discountInPercent, new { htmlAttributes = new { @class = "form-control discountInPercent", @autocomplete = "off", @placeholder = "Disc %", id = "discountInPercent" + i.ToString(), @data_val = "false" } })
                </td>
                <td>
                    @Html.EditorFor(model => model.orderItems[i].discountInAmount, new { htmlAttributes = new { @class = "form-control discountInAmount", @autocomplete = "off", @placeholder = "Disc %", id = "discountInAmount" + i.ToString(), @data_val = "false" } })
                </td>
                <td valign="top">
                    <input type="button" class="btn-size btn btn-danger btn-sm btnDelRow" value="Del" id="btnDelRow" />
                </td>
            </tr>
        }
    </tbody>
</table>

导致验证绕过的字段代码

@Html.EditorFor(model => model.orderItems[i].quantity, new { htmlAttributes = new { @class = "form-control quantity", @autocomplete = "off", @placeholder = "Qty", id = "quantity" + i.ToString(), @data_val = "false" } })

恳请资深技术人士协助解决此问题。

内容的提问来源于stack exchange,提问作者fungus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 17:20:59