简体   繁体   中英

ASP.NET MVC5 : How do I pass data to a view?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM