I have a if elseif else statement like as follows
if (check1) ---- 1
{
if(check2) ----2
{
}
}
else if (checkother1) ---- 3
{
if(checkother2) -----4
}
else ------- 5
{
do this
}
return someview
when the code hits first if statement and if the condition validates it will go inside check2 if statement after doing the validation it is entering the last else statement(5) correctly .
But when the code enters the 3rd if statement and after validating the 4th if statement if is not validated it is showing error properly but if it get validated it is directly going to the return
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("CanditateId,CanditateFirstName,CanditateLastName,CanditateAadhar,CanditatePancard,CanditateEmail,CanditateGender,CanditateLocation,CanditateMaritialStatus,CanditateDoj,CanditateActDoa,CanditateDesignation,CanditateDepartment,CanditateTeam,CanditateAddress,CanditateAltNumber,CanditateIdNumber,CanditateBaseLocation,CanditateManager,CanditatePrefix,CanditateMobile")] Canditateinfo canditateinfo)
{
var canditateinfos = await _context.Canditateinfos.FindAsync(id);
if (id != canditateinfo.CanditateId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
var isAAdharAlreadyExists = _context.Canditateinfos.Any(x => x.CanditateAadhar == canditateinfo.CanditateAadhar);
var isPanCardAlreadyExists = _context.Canditateinfos.Any(x => x.CanditatePancard == canditateinfo.CanditatePancard);
if (canditateinfos.CanditateAadhar != canditateinfo.CanditateAadhar)
{
if (isAAdharAlreadyExists)
{
ModelState.AddModelError("CanditateAadhar", "User with this aadhar already exists");
return View(canditateinfo);
}
}
else if (canditateinfos.CanditatePancard != canditateinfo.CanditatePancard)
{
if (isPanCardAlreadyExists)
{
ModelState.AddModelError("CanditatePancard", "User with this Pan Number already exists");
return View(canditateinfo);
}
}
else
{
canditateinfo.EnteredBy = canditateinfos.EnteredBy;
canditateinfo.EnteredDate = canditateinfos.EnteredDate;
canditateinfo.DeleteFlag = canditateinfos.DeleteFlag;
canditateinfo.IsActive = canditateinfos.IsActive;
canditateinfo.CanditateId = canditateinfos.CanditateId;
canditateinfo.UpdatedBy = HttpContext.Session.GetString("username");
canditateinfo.UpdatedDate = DateTime.Now;
_context.Update(canditateinfo);
await _context.SaveChangesAsync();
TempData["SuccessMessage"] = "Canditate Updated Successfully";
return RedirectToAction(nameof(Index));
}
}
catch
{
}
}
return View(canditateinfo);
}