title: Add error message if duplicate record exist in MVC Popup Form
body: <p>I have a popup window called AddNew will display when the user click a button called add New. The button itself has number of field called TRN and DOB. The problem that i have now that i am trying to display error message when the user try to add duplicate TRN but unfortunately no error message display only if the required field is empty.</p>
  
  <p>Is there anyway i could display the duplicate error message in validation summary like displaying in required field and hold all the previous entered value in the form. Please advise. Thank you</p>
  
  <p>Here is the code that i have so far. Thank you</p>
  
  <p><strong>Index.cshtml</strong></p>
  
  <pre><code>&lt;input type="button" id="btnAddNew" style="height:50px; font-size:14px; width:95px; background-color:#3399FF; white-space: normal;" class="k-button" title="AddNew" value="Add New" /&gt;
  
  &lt;script type="text/javascript"&gt;
  
  $(document).ready(function () {
      $('#btnAddNew').click(function () {
       window.open('@Url.Action("AddNew", "Details")', 'AddProduct', 'height=' + (window.screen.height - 450) + ',width=950,left=' + (window.screen.width - 5) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');
          });
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>AddNew.cshtml (window popup)</strong></p>
  
  <pre><code>@{
      Layout = "~/Views/Shared/_LayoutNoMenu.cshtml";
  
  }
  
  &lt;script src="../../Scripts/jquery.validate.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"&gt;&lt;/script&gt;
  
  @model HHIMS_Web_App.Models.CModel
  @{
      ViewBag.Title = "New Product";
  }
  
  @using (Html.BeginForm("Create", "Details", FormMethod.Post, new { id = "frmAsset" }))
   {
      ViewContext.FormContext.ValidationSummaryId = "valSumId";
      @Html.ValidationSummary(false, "Please fix the following errors:", new Dictionary&lt;string, object&gt; { { "id", "valSumId" } });
  
      &lt;fieldset id="AddNew"&gt;
  
  
          &lt;div&gt;
              &lt;div class="addInfo"&gt;
                  @Html.LabelFor(model =&gt; model.TRN)
                  @Html.EditorFor(model =&gt; model.TRN)
                  @Html.ValidationMessageFor(model =&gt; model.TRN, "*")
              &lt;/div&gt;
               &amp;nbsp;
              &lt;div class="AddSmallBox"&gt;
                  @Html.LabelFor(model =&gt; model.DOB)
                  @Html.EditorFor(model =&gt; model.DOB)
                  @Html.ValidationMessageFor(model =&gt; model.DOB, "*")
  
              &lt;/div&gt;
       &lt;/div&gt;
  
  
  
  
  
  
           &lt;div&gt;
                   &lt;div class="smallAddAndCancel"&gt;
                      &lt;input type="button" id="btnCancel" style="height:50px; width:85px; font-size:14px; background-color:#3399FF" class="k-button" title="Cancel" value="Cancel" onclick="window.close()" /&gt;
                      &lt;input type="submit" id="btnSave" style="height:50px; width:85px; font-size:14px; background-color:#3399FF;white-space: normal" class="k-button" title="Save" value="Save"/&gt;
  
                  &lt;/div&gt;
          &lt;/div&gt;
  
  
      &lt;/fieldset&gt;
  }
  
  
  &lt;script type="text/javascript"&gt;
  
  
      $(document).ready(function () {
          $("#datepicker").closest("span.k-datepicker").width(400);
  
          $('#btnSave').click(function () {
  
             var validation = $("#frmAsset"); 
              if (!validation.valid()) {
                  return false;
              else
              {
  
  
  
                      $.ajax({
                          type: 'POST',
                          url: "@Url.Action("Create","Details")",
                          data: {
  
                              TRN: $("#TRN").val(),
                              DOB: $("#DOB").val(),
  
  
  
                          },
  
                              success: function () {
                              window.close()
                          }
                      });
              }
          });
  
  
      });
  
  
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Controller:</strong>
  [HttpPost]</p>
  
  <pre><code>public ActionResult Create(Cmodel)
      {
          try
          {
              if (ModelState.IsValid)
              {
                  CheckDuplicateHRNExist(model);
                  return RedirectToAction("AddNew");
              }
              if (ModelState.IsValid)
              {
                  HH_DataAccessLayer.Consumers dalModel = new HH_DataAccessLayer.Consumers();
                  Mapper.CreateMap&lt;CModel, HH_DataAccessLayer.Consumers&gt;();
                  Mapper.Map(model, dalModel);
                  return RedirectToAction("Index");
              }
          }
          catch (DbUpdateConcurrencyException e)
          {
              var entry = e.Entries.Single();
              var clientValues = (CModel)entry.Entity;
          }
          return RedirectToAction("Index");
      }
  
      /// &lt;summary&gt;
      /// Validate TRN find if no duplicate TRN recorded
      /// &lt;/summary&gt;
      /// &lt;param name="model"&gt;&lt;/param&gt;
  
  
  private void CheckDuplicateTRNExist(CModel model)
      {
          HEntities context = new HEntities();
          if (model.TRN != null)
          {
              var duplicateTRN = context.Consumers.Where(d =&gt; d.TRN == model.TRN).FirstOrDefault();
              if (duplicateTRN != null)
              {
                  var errorMessage = String.Format("TRN is already exist.", duplicateTRN);
                  ModelState.AddModelError(string.Empty, errorMessage);
              }
          }
      }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>[Required(ErrorMessage = "Please enter TRN")]
          [DisplayName("TRN")]
          [StringLength(20)]
          public string TRN { get; set; }
  
  [Required(ErrorMessage = "Please enter or select Date of Birth")]
          [DisplayName("Date Of Birth")]
          public DateTime? DOB { get; set; }
  </code></pre>