0

i have a table rubriqueintermediare with two fields rubriqueId and rubriqueintermediareId which is a foreign key from the table Rubrique.

My table Rubriqueintermediare have a listbox of the libelle field from the rubrique table.

When I add a new rubriqueintermediare I mustn't add an existing one in the rubriqueintermediare list.

How could I do this validation when I have just rubriqueintermediareId and rubriqueId in the table rubriqueintermediare which are auto incremented in my sql server. ther is my methode add in the controller of rubriqueintermediare:

[HttpPost]
    public JsonResult Save([DataSourceRequest] DataSourceRequest dsRequest, RubriqueIntermediareVM vm)
    {
        try
        {
            ViewData["CodeRubrique"] = new SelectList(RefDataManager.GetRefData<RubriqueVM>(), "RubriqueId", "CODELIBELLE");
            if (ModelState.IsValid)
            {
                RubriqueIntermediareVM rubi = ServiceApplicatif.Save(vm);
                IEnumerable<RubriqueVM> rubrique = RefDataManager.GetRefData<RubriqueVM>() as IEnumerable<RubriqueVM>;
            }
            DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID);
            dataCache.DropDataCache<RubriqueIntermediareVM>();
            return Json(new[] { vm }.ToDataSourceResult(dsRequest, ModelState));
        }
        catch (Exception ex)
        {
            LoggerRubriqueIntermediare.Error(string.Format("Exception : {0}", ex.Message.ToString()));
            throw new Exception("Erreur lors de l'enregistrement.");
        }
    }
smarttech
  • 133
  • 1
  • 4
  • 11
  • Instead of _describing_ the code, could you _show_ us a short code example? – gunr2171 Apr 28 '14 at 13:22
  • there is my methode add in the controller of Rubriqueintermediare: – smarttech Apr 28 '14 at 13:34
  • if (ModelState.IsValid) { RubriqueIntermediareVM rubi = ServiceApplicatif.Save(vm); IEnumerable rubrique = RefDataManager.GetRefData() as IEnumerable; } DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID); dataCache.DropDataCache(); return Json(new[] { vm }.ToDataSourceResult(dsRequest, ModelState)); – smarttech Apr 28 '14 at 13:39
  • Please edit your post to include the code. We can't easily read code in the comments. – gunr2171 Apr 28 '14 at 13:39
  • okey @gunr2171 i have to add to my save () methode a validation test . i musn't add (from my listbox )a new exisiting Rubriqueintermediare – smarttech Apr 28 '14 at 13:45

1 Answers1

0

You have to do this validation into your database

In store procedure

if exists( select Count(id) 
           from Rubriqueintermediare 
           where @name=rubricname) -- Assuming rubricname is column  
begin
    --return field as you want 
END
Else
Begin
    --Insert as you want
End
gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • i have in my RUBRIQUEINTERMEDIARE table just RUBRIQUEID and RUBRIQUEINTERMEDIAREID i haven't any other field – smarttech Apr 28 '14 at 15:57