I am developing an ASP.NET MVC 5 application with C # and the .NET Framework 4.7.
In my opinion, I have this code:
<tbody> @for (int index = 0; index < Model.Products.Count; index++) { <tr> <td> <div class="group"> @Html.TextBoxFor(m => m.Products[index].ProductCode) @Html.HiddenFor(m => m.Products[index].Law) <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].ProductCode)</div> </div> </td> <td> <div class="group"> @Html.TextBoxFor(m => m.Products[index].Description, new { @class = "productClass", @style = "max-width:none" })<br /> <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Description)</div> </div> </td> <td> <div class="group"> @Html.TextBoxFor(m => m.Products[index].Name, new { @class = "productClass" })<br /> <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Name)</div> </div> </td> <td> <div class="group"> @Html.TextBoxFor(m => m.Products[index].Comment, new { @class = "productClass" })<br /> <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Comment)</div> </div> </td> </tr> } </tbody>
I have a button that adds new rows to a table using jQuery.
I need to check if the ProductCode unique (all products must have a unique product code). I did this using jQuery using the equivalent dictionary data structure, but I'm not sure if I can do this using ASP.NET validation.
I want to show a message similar to the message that shows when the ProductCode field is empty.
The model I'm using in this view is:
public class CreateProductViewModel { public byte LawId { get; set; } public IList<Models.Products> Products { get; set; } public CreateProductViewModel() { Products = new List<Models.Products>(); } }
And Products has the following checks:
public class Products { public int Id { get; set; } [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "ProductCodeRequired")] [StringLength(20, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "ProductCodeLong")] public string ProductCode { get; set; } [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "ProductDescriptionLong")] public string Description { get; set; } public byte Law { get; set; } [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "ProductNameLong")] public string Name { get; set; } [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "ProductCommentLong")] public string Comment { get; set; } }
Is there a way to add confirmation to verify that all product codes are unique on the client side?
In my database table, I have this limitation, but it is on the server side:
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [UNQ_PRODUCTCODE_PRODUCT] UNIQUE (ProductCode)