title: How to prevent saving of duplicate data?
body: <p>I am developing MVC app.</p>
  
  <p>I am using Jquery in Creat-Veiw for validate the data.</p>
  
  <p>I am adding the Role and checking whether that Role(or you can say UserName) already exists in DB or not.
  If Role already exists then validation message should display and should not be allow to add in db (should not allow to save).</p>
  
  <p>I have a text-box which accepts the Role, on the blur function I have checked whether Role already exists or not.</p>
  
  <p>Every thing working ok, If Role already exists then validation message comes up. 
  But after clicking on save button it saves in db.</p>
  
  <p>I want to prevent it ? How to do this ? </p>
  
  <p>I have below code of Create View.</p>
  
  <pre><code>    @model IEnumerable&lt;PaymentAdviceEntity.Role&gt;
      &lt;div id="roleList"&gt;
      &lt;div class="span6"&gt;
      &lt;div class="span12 HeaderField2"&gt;
          Roles
      &lt;legend style="margin-bottom:2px;margin-top:5px;"&gt;&lt;/legend&gt;
      &lt;/div&gt;
  
      &lt;div  class="span12"&gt;    
  
  
  
  
          &lt;div style="display:inline-block"&gt;&lt;/div&gt;
          &lt;div id="addrole" style="display:none"&gt;
          &lt;span&gt; @Html.TextBox("RoleName", String.Empty, new { @id = "RoleName",style="margin-bottom:0px;" })&lt;/span&gt;
  
             &lt;span&gt; 
                 &lt;input type="button" value="Save" id="btnSave"/&gt;
                  &lt;input type="button"  value="Cancel" id="btnCancel" /&gt;
             &lt;/span&gt;
        &lt;/div&gt;    
          &lt;div style="margin-top:5px;"&gt;
   &lt;span id="RoleNameValidation" style="display:none;color:Red;"&gt;Role already exists&lt;/span&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>For this I am using the below Jquery.</p>
  
  <pre><code> $("#RoleName").blur(function ()
      {
          var Role_Name = $('#RoleName').val();
          //alert(RoleName);
          var url = "@Html.Raw(Url.Action("checkForUniqueName","Role",new {@RName = "RoleName"}))";
          url = url.replace("RoleName", Role_Name);
          $.post(url, function (data)
          {
              if (data == false) {
  
                  $("#RoleNameValidation").show();
                  $('#RoleName').focus();
              }
              else {
                  $("#RoleNameValidation").hide()
              }
          });
      });
  </code></pre>
  
  <p>and the controller Code is....</p>
  
  <pre><code> public ActionResult checkForUniqueName(string RName)
           {
              bool Valid = false;
  
              var RoleList = from e in db.Roles
                             where e.Name.Equals(RName)
                             select e;
              if (RoleList.Count() &gt; 0 )  
              {
                  Valid = false;
              }
              else
              {
                  Valid = true;
              }
  
              return Json(Valid, JsonRequestBehavior.AllowGet);
  
  
           }
  </code></pre>