0

I have a problem with this action:

"activateOffreLocation" checks the value of "Publication_Statut" and change it ( if it equals "Activée", the action change it to "Désactivée" and vice versa

i don't get any error ! it switch simply to the " listOffreLocation" view after asking the confirmation !

i tried to follow the execution with break Points, it turned that in my Post Action the "offreLocation" is null, while the ID is not, it contains the right value of the selected ITEM in the first view.

         // GET: /OffreLocation/Activate
    public ActionResult ActivateOffreLocation(int id)
    {
        return View(db.PublicationSet.Find(id));
    }


    // POST: /OffreLocation/Activate
    [HttpPost]
    public ActionResult ActivateOffreLocation(int id, OffreLocation offreLocation)
    {
        try {
            var statut = offreLocation.Publication_Statut;

               if(String.Equals(statut,"Activée"))
                        {
                            offreLocation.Publication_Statut = "Désactivée";
                            db.SaveChanges();
                            return View();
                        }
            else 
                        {
                            offreLocation.Publication_Statut = "Activée";
                            db.SaveChanges();              
                            return RedirectToAction("ListOffreLocation"); 
                        }              
             } 
        catch (Exception exp)
        {
            Console.WriteLine("IOException source: {0}", exp.Source);
            return RedirectToAction("Error"); 
        }                      
    }

in my view " listOffreLocation " i just defined the link to the "activateOffreLocation" view:

@Html.ActionLink("Désactiver", "ActivateOffreLocation", new { id = item.Publication_ID }) | 

"ActivateOffreLocation" View:

<fieldset style="border:dashed; margin-top:80px">
<h3 style="text-align: center; margin-left:80px; margin-top:80px">  Voullez vous vraiment désactiver cette offre de location?   
</h3>
<h4 style="text-align:center">  Num:    @Html.EditorFor(model => model.Publication_ID) </h4>
<h4 style="text-align:center">  Statut    @Html.EditorFor(model => model.Publication_Statut) </h4>
    <div style="margin-bottom:80px">

        <hr />


        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(model =>model.Publication_ID) 
            <div class="form-actions no-color" style="text-align:center">
                @Html.HiddenFor(model => model.Publication_ID)
                <input type="submit" value="Désactiver" class="btn btn-default" /> |
                @Html.ActionLink("Retour à la  Liste", "ListOffreLocation")

            </div>
        }
    </div>
 </fieldset>
404NotFound
  • 635
  • 1
  • 6
  • 14

1 Answers1

0

First of all, you aren't showing that you are passing any model to your view, "ActivateOffreLocation." You also don't appear to be sending any model data, your model being "OffreLocation" to your Action. This could be your problem.

If you fix this, and it doesn't solve your issue, post more code and I'll update my answer.

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • yes i'm using my model in the "ActivateOffreLocation" view as bellow : @model COM.MENNILIK.Models.OffreLocation – 404NotFound May 23 '14 at 12:43
  • what i did, i tried to display the status and the ID of the publication in the activate view using the get method, it works. but once i click in the submit button, it loses the object so i can't change anything in my database ( the post action can't get the object), so i wanna know how to pass this object from my activate view to the post Activate action( syntax) – 404NotFound May 23 '14 at 12:48
  • You are trying to pass a value from your submit button; I recommend using a hidden input, instead. To pass a value from a submit button read this http://stackoverflow.com/questions/1714028/mvc-which-submit-button-has-been-pressed – user1477388 May 23 '14 at 13:27
  • i used the hidenInput and i also tried the procedure described in ur link! still getting a null object in the post action : public ActionResult ActivateOffreLocation(int id, OffreLocation offreLocation) – 404NotFound May 23 '14 at 14:28
  • Is your entire offreLocation object null or just that property? – user1477388 May 23 '14 at 15:27
  • no the entire object,because once i click the submit button i change the view so he can`t remember the object – 404NotFound May 24 '14 at 21:39
  • Try only passing the model `public ActionResult ActivateOffreLocation(OffreLocation offreLocation)` and put a breakpoint on that line to see if the model is null after you execute the line. – user1477388 May 24 '14 at 21:43
  • yes i did it ! qnd this is why i say that my object is null – 404NotFound May 25 '14 at 22:08