i have a table Rubriqueintermediare with a rubriqueIntermediareId and rubriqueId as a foreign key from the table Rubrique:So in my rubrique intermediare i have a listbox of rubrique i want to compare viewdata list values with dropdownlist selected so to interdict the addition of the same rubriqueintermediare : i tried this code but it didn't work :
[HttpPost]
public JsonResult Save([DataSourceRequest] DataSourceRequest dsRequest, RubriquepointageVM vm)
{
try
{
IEnumerable<RubriqueVM> listRubrique = RefDataManager.GetRefData<RubriqueVM>() as IEnumerable<RubriqueVM>;
ViewData["CodeRubrique"] = new SelectList(RefDataManager.GetRefData<RubriqueVM>(), "RubriqueId", "CODELIBELLE");
var Rubriques = listRubrique.AsQueryable();
List<RubriquepointageVM> lstRubriqueinter = ServiceApplicatif.GetListe();
if (!(listRubrique.Any(r => r.RubriqueId==(vm.RubriqueId)))){
ModelState.AddModelError("RubriqueId", "ajout impossible ");
return Json(ModelState.ToDataSourceResult());
}else {
RubriquepointageVM rub = ServiceApplicatif.Save(vm);
return Json(new[] { vm }.ToDataSourceResult(dsRequest, ModelState));
DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID);
dataCache.DropDataCache<RubriquepointageVM>();
}
}
catch (Exception ex)
{
LoggerRubriquepointage.Error(string.Format("Exception : {0}", ex.Message.ToString()));
throw new Exception("Erreur lors de l'enregistrement.");
}
}
when i put a break point if i have added the same value from the listbox or not it excutes just the else:
else {
RubriquepointageVM rub = ServiceApplicatif.Save(vm);
return Json(new[] { vm }.ToDataSourceResult(dsRequest, ModelState));
DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID);
dataCache.DropDataCache<RubriquepointageVM>();
}