5

I'm developing an ASP.NET MVC 5 application with C# and .NET Framework 4.7.

On my view 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 add new rows to the table with jQuery.

I need to check if ProductCode is unique (all products must have an unique product code). I did it with jQuery using a dictionary equivalent data structure but I'm not sure if I can do it with an ASP.NET validation.

I want to show a message like the message it shows when 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 these validations:

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 a validation to validate that all Product's codes are unique on client side?

On my database table I have this constraint, but it is on server side:

ALTER TABLE [dbo].[Product]
    ADD CONSTRAINT [UNQ_PRODUCTCODE_PRODUCT]
    UNIQUE (ProductCode)
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 5
    To check for uniqueness compared with other existing records, you can use the `RemoteAttribute`. But to additional compare for uniqueness within the same view as you dynamically add new records, you will need to write your own custom validation attribute that implements `IClientValidatable` for both client and server side validation. –  May 25 '17 at 07:08
  • You can use validation attribute https://stackoverflow.com/a/5146766/2630817 – Just code May 26 '17 at 06:24

1 Answers1

1

Hi first of all to answer to your question,

Is there a way to add a validation to validate that all Product's codes are unique?

yes there is way..:)

And Here is the way:

 [Index("Ix_ProductCode",Order =1,IsUnique =true)]
        public string ProductCode { get; set; }

By using index you can achieve it, I personally implemented in my project it worked well.

Useful Link:

https://www.codeproject.com/articles/1130113/best-ways-of-implementing-uniqueness-or-unique-key

Offical explanation of index can be found here : https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx

Hope the above information was useful, kindly let me know your thoughts or feedbacks

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
  • 1
    Thanks, but I want to do it on the client side, before I send the data to the server. Thanks. – VansFannel May 26 '17 at 06:14
  • Hi My pleasure and happy that it helped, I dont think we can implement the funtionality to check uniqueness, fully on the client side as per my knowledge,because to fetch the data from db we should go the serverside anyhow .but may be you can do one thing , trap the exception for the above annotation and show the error message on the client. so user will see the same screen with error message in client side. thanks – Karthik Elumalai May 26 '17 at 15:55