Waiting for replying
Bypass validations in ASP.NET MVC 5
I am facing an issue with one of my fields that uses EditorFor()
a data type of INT
. This field is not required on the server side, but form validations are being bypassed due to its presence, 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 SalamCoManagementSystem.Models
{
public class OrderViewModel
{
public OrderViewModel()
{
orderItems = new List<OrderItemViewModel>();
orderItems.Add(new OrderItemViewModel());
}
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; }
}
Dynamic rows and fields
<table class="tableTable table-bordered" id="orderDetails" width="100%">
<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" } })
Developer technologies | ASP.NET | Other
3 answers
Sort by: Most helpful
-
-
Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
2024-01-11T20:20:42.3+00:00 Any non nullable value type like int, float, DateTime is required. If you don’t want the int field required, you must make it nullable (int?)
-
Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
2024-01-12T07:40:27.1166667+00:00 Hi @Nomee Cool, Here are a few things you can try:
- Make the property as Nullable like:
public int? quantity { get; set; }
- To turn this off add
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
to Application_Start. - Use the below in controller class
ModelState["quantity"].Errors.Clear();
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. - Make the property as Nullable like: