繁体   English   中英

在同一控制器中的两个ActionResult之间传递数据,从而在ASP.NET MVC中呈现不同的视图

[英]Passing data between two ActionResults in the same controller rendering different views in asp.net mvc

我必须将iddemande从一个ActionResult传递到同一控制器中的另一个,但是由于TempData或Session失败,因为每个ActionResult呈现不同的视图。 因此,我需要您的帮助来了解如何在这两个控制器之间从“详细信息”到“ Sortie”传递数据。

我的控制器:

    public ActionResult Details(int? num,int iddemande)
        {
            TempData["num"] = num;
            //Session["iddemande"] = iddemande;
            TempData["iddemande"] = iddemande;
            if (num == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var listdemandes = (from d in db.Demande_Gabarit
                                join g in db.Gabarits
                                    on d.idPoste equals g.idPoste into ThisList
                                from g in ThisList.DefaultIfEmpty()
                                select new
                                {
                                    NumDemande = d.NumDemande,
                                    Emetteur = d.Emetteur,
                                    Date = d.Date,
                                    Quantite = d.Quantite,
                                    Designation = g.DesignationGabarit,
                                    Produit=d.Postes.Produits.Reference,
                                    Ligne = d.Ligne.designation
                                }).ToList().Select(x => new DemandeViewModel()
                                {
                                    NumDemande = x.NumDemande,
                                    Emetteur = x.Emetteur,
                                    Date = x.Date,
                                    Quantite = x.Quantite,
                                    DesignationGabarit = x.Designation,
                                    designation = x.Ligne,
                                    Reference= (int)x.Produit
                                }).Where(x => x.NumDemande == num);


            return View(listdemandes.DistinctBy(x=>x.DesignationGabarit));

        }
//Get Sortie
        public ActionResult Sortie(Int64 id)
        {
             TempData["codebarre"] = id;

            return View();

        }
        //Post Sortie
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Sortie(DemandeViewModel postData)
        {
            Int64 cb = postData.CodeBarre;

            var mvtrepository = new MvtRepository(db);
            var gabaritrepository = new GabaritRepository(db);





            if (Convert.ToInt64(TempData["codebarre"]) == cb)
            {
                var mvtInsert = mvtrepository.InsertMvt(DateTime.Now, Convert.ToInt64(TempData["codebarre"]), 2);
                var idDemande = Convert.ToInt32(TempData["iddemande"]);
                Demande_Gabarit demande =
                    demanderepository.Get(x => x.id_demande == idDemande).SingleOrDefault();

                demande.QtLivree = 1;
                demanderepository.Update(demande);

                return RedirectToAction("Details");
            }


            else
            {
                return View("Error");
            }

        }

TempData值仅可用于下一个请求。 这意味着,如果您为Details动作设置了一些值,然后导航至Sortie GET动作,则可以检索它。 但是,如果您提交表单并单击Sortie HTTPPost操作,它将不可用。

一种选择是再次设置tempdata值,以便在下一个请求中也可以访问它。

public ActionResult Sortie(Int64 id)
{       
    TempData["codebarre"] = id; 
    var t = TempData["iddemande"];
    TempData["iddemande"] = t;
    return View();
}

另一个选项(更好/更干净的选项恕我直言)是通过表单传递此值。 因此,无需在GET操作中再次设置TempData,而是将这个TempData值保留在一个form元素内,并在提交表单时,将该值发送到HttpPost操作方法。 您可以在操作方法中添加参数以阅读该内容。

因此,在您的Sortie视图表单中,

@using (Html.BeginForm())
{
    <input type="text" value="@TempData["iddemande"]" name="iddemande" />
    <input type="submit" value="submit" />
}

然后在您的http发布操作中

[HttpPost]        
public ActionResult Sortie(DemandeViewModel postData,int iddemande)
{
   // You can use the value of iddemande
   // to do  : Return something
}

关于会话,只要您正确阅读它,它就应该起作用。 会话值在设置后将可用于所有请求,直到会话结束。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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