Bypass validations in ASP.NET MVC 5

37 views Asked by At

I am facing an issue with one of my fields that uses EditorFor() with a data type of INT. This field is not required on the server side, but due to its presence, form validations are being bypassed, and the form submission is being executed. If I temporarily remove this field, form validations work fine, but adding it back causes the form validation to be bypassed again.

I have been stuck in this problem for quite some time, and despite extensive research and development, I haven't found a solution. Now, I am posting my problem on this forum for assistance.

Model

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 Html

<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>

Because of this field form by-pass validations

@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" } })

There are many respected experienced individuals on this forum who can help me solve this issue.

0

There are 0 answers