Search code examples
javascriptjqueryasp.net-mvc-4client-side-validationmvc-editor-templates

how to insert only unique value in Editor templates in mvc


i want to enter only unique Machine_serial_no in my editor templates , user can add Machine_serial_no through browsing a file or can enter manually As shown in below code, i just want to make sure that user shouldnot allow to enter same value twice .your suggestion is always welcome ..Thanks in advance..

//main view

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.9.2.min.js"></script>

<div id="cast">                                               

                <tr>
                    <td> File:</td>
                    <td><input type="file" id="file" /> </td>
                    <td>   <input type="button" value="Upload" id="btnSubmit" /> </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>

                <tr class="manualSerial">
                    <td class="required">Total No of serial no U want to enter:</td>
                    <td>@Html.TextBoxFor(x => x.count, new { @Value = 0 })</td>
                    <td colspan="4">
                        <input type="button" value="Add Serial" id="addserial" />
                        @*@Html.ActionLink("Add Serial", "AddMachineSerial", "Import", new { @id = "addserial" ,})
                            @Html.ValidationMessageFor(model => model.serials.Machine_serial_no)*@
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td id="ShowModel" colspan="6">
                        <table id="tbl1" style="width:100%;">
                            <thead>
                                <tr>
                                    <td>Brand</td>
                                    <td>Machine</td>
                                    <td>Model</td>
                                    <td>Serial No</td>
                                    <td>Mac Address</td>
                                    <td>Action</td>
                                </tr>
                            </thead>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td id="ShowModel" colspan="6">
                        <div style="height:253px; width:100% ;overflow: auto;">
                            <table style="width:100%;margin-left:0px;margin-right:0px">
                                @*<thead>
                                        <tr>
                                            <th style="width:45px;">Brand</th>
                                                <th style="width:90px;">Machine</th>
                                                <th style="width:80px;">Model</th>
                                                <th>Serial No</th>
                                                <th>Mac Address</th>
                                                <th>Action</th>
                                        </tr>
                                    </thead>*@

                                <tr>
                                    <td colspan="6" id="td_serial"></td>
                                </tr>

                            </table>
                        </div>
                    </td>
                </tr>                   

//jquery

$('#addserial').click(function () {

    var count = $('#count').val();
    var i;    
        if ($('#searchid').val() != '') {
            if ($('#count').val() != 0) {
                for (i = 0; i < count; i++) {
                    $.ajax({
                        type: 'GET',
                        data: { mid: $('#machineTypes_MTId').val(), modelName: $('#searchid').val(), modelId: $('#searchValue').val() },
                        url: '@Url.Action("AddMachineSerial","Import")',
                        success: function (response) {
                            $('#ShowModel').show();
                            $('#td_serial').prepend(response);
                            $('#count').val(0);
                        }
                    });
                }
            }
            else {
                alert("Enter no of serial you want to enter!")
            }
        }
        else {
            alert("select Model First!")
            $('#count').val(0);
        }      

});

//editor Templates /parital view

    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginCollectionItem("serialList"))
{
    @Html.ValidationSummary(true)
    <div id="DeleteTxt1">

        <table id="tbl1" style="width:100%;margin-left:0px;margin-right:0px">
            <tr class="importitem1">
                <td>@Model.brandName</td>
                <td>@Model.machineName</td>
                <td>@Model.MachineModel</td>
                @*<td class="required">Machine Serial No.:</td>*@
                <td>

                    @Html.TextBoxFor(x => x.Machine_serial_no, new { placeHolder = "Enter Machine Serial here.", @class = "serial1"})
                    @Html.ValidationMessageFor(x => x.Machine_serial_no)
                </td>


                <td><input type="button" value="Cancel" id="DeleteBtn1" style="color:red;" /></td>

            </tr>
        </table>
    </div>
}

Solution

  • Assuming you want to alert the user as soon as they enter a non unique value, then you need to handle the .change() event of the textbox, and since the textboxes are being added dynamically, you need to use event delegation

    $('#td_serial').on('change', '.serial1', function() {
      var isvalid = true;
      var inputs = $('.serial1').not($(this));
      var text = $(this).val();
      $.each(inputs, function(index, item) {
        if ($(this).val() && $(this).val() === text) {
          isvalid = false;
          return false;
        }
      });
      if (!isvalid) {
        alert('not unique!');
      }
    });
    

    Note the alert('not unique!') is just for testing purposes - its not clear how you want to notify the user - e.g. include a div with an error message that your might show/hide as required

    Next your partial includes @Html.ValidationMessageFor(x => x.Machine_serial_no) suggesting you have validation attributes. In order to get client side validation for dynamically created elements you need to re-parse the validator when the new elements have been added. Modify your script to

    for (i = 0; i < count; i++) {
      $.ajax({
        type: 'GET',
        data: { mid: $('#machineTypes_MTId').val(), modelName: $('#searchid').val(), modelId: $('#searchValue').val() },
        url: '@Url.Action("AddMachineSerial","Import")',
        success: function (response) {
          $('#ShowModel').show();
          $('#td_serial').prepend(response);
          $('#count').val(0);
          // Reparse the validator
          $('form').data('validator', null);
          $.validator.unobtrusive.parse(form);
        }
      });
    }
    

    Side note - consider modifying this so that you only re-parse once all ajax calls are complete

    Note also that you should only have one @Html.ValidationSummary(true) (your currently is adding one for each partial) and you need to remove the scripts from the partial - your should have one copy of the scripts only in the main view