0

In my controller, I have two method like that :

 public ActionResult NouvelleDemande()
    {

        int NumDossier = StructureData.DonneNumDossier((string)Session["Utilisateur"], (string)Session["MotDePasse"]);
        List<Contact> ListeContacts = StructureData.DonneListeContact(NumDossier);
        if (ListeContacts != null)
        { ViewBag.ListeContacts = ListeContacts; }
        else
        { ViewBag.ListeContacts = null; }
        return View();
    }

    public ActionResult NouvelleDemande(DemandeAssistance nouvelleDemande)
    {
        bool DemandeEnregistree = nouvelleDemande.EnregistrerDemande();
        if (DemandeEnregistree)
        { 
            return Index();
        }
        else
        {
            ViewBag.Error = "La demande n'a pas été enregistrée !";
            return View();
        }
    }

So when I want just to display the view() associated to the method, I call the first one. In the view(), I have a form which when submitted, send an object DemandeAssistance to the second method. In the routes config, i did that :

routes.MapRoute(
            name: "NouvelleDemande",
            url: "{controller}/{action}",
            defaults: new { controller = "Accueil", action = "NouvelleDemande" }
        );

        routes.MapRoute(
            name: "AjouterNouvelleDemande",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Accueil", action = "NouvelleDemande", id = UrlParameter.Optional }
        );

But it shows me an error when I want just to display the view saying that there is a misunderstanding between these two routes. What did I do wrong ?

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63

1 Answers1

0

I manage to find out what was missing even if I don't really understand why. I've just put that :

// POST : /Accueil/NouvelleDemande
    [HttpPost]

Upon the method which contains a parameter, as follows:

// POST : /Accueil/NouvelleDemande
    [HttpPost]
    public ActionResult NouvelleDemande(DemandeAssistance nouvelleDemande)
    {
        bool DemandeEnregistree = nouvelleDemande.EnregistrerDemande();
        if (DemandeEnregistree)
        { 
            return Index();
        }
        else
        {
            ViewBag.Error = "La demande n'a pas été enregistrée !";
            return View();
        }
    }

Maybe those interested will have time to explain why it works actually.

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63