function btnNextAvailable_OnClick() {
var randomstring=GenerateRandonString();
while(IsRandomStringExists(randomstring))
{
document.getElementById("SequentialId").value = randomstring
}
document.getElementById("SequentialId").value = randomstring
}
function IsRandomStringExists(randomstring)
{
$.get('Home/CheckStringExists/'+randomstring ,function(response)
{
if(response=="true")
{
return true;
}else
{
return false;
}
});
}
function GenerateRandonString()
{
$("#nextAvailableButtonClick.val('true')");
var mode = $.hash.getValue("m");
var chars = "0123456789";
var stringLength = 3;
var randomstring = '';
for (var i = 0; i < stringLength; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
return randomstring;
}
Also create "CheckStringExists" ActionMethod in Home controller or in your controller with parameter.
In HomeController
public JsonResult CheckStringExists(string id)
{
//Write database logic here
return Json((object)"true", JsonRequestBehavior.AllowGet);
}
In HomeController
public JsonResult CheckStringExists(string id)
{
var customer = ObjectContext.Companies.OfType<TrueTracEntities.Customer>().Where(r => r.SequentialId.Equals(id)).FirstOrDefault();
return Json((object)"true", JsonRequestBehavior.AllowGet);
}