2016-04-15 22 views
0

私はオブジェクトまたは少なくともそのIDをコントローラから別のコントローラに送信したいのですが、どうすればいいですか? asp.net mvcでコントローラから別の変数に変数を送信する方法コントローラ間の通信asp.net mvc by viewdata

この指示は、この問題を解決しているのですか? これは最初のコントローラのメソッドです:

[HttpPost] 
 
     [ValidateAntiForgeryToken] 
 
     public ActionResult login(Utilisateur u) 
 
     { 
 
      if (u.login != null && u.Password != null) 
 
      { 
 
       using (BD_GestionDepences db = new BD_GestionDepences()) 
 
       { 
 
        string x; 
 
        string hash = GetSHA1HashData(u.Password); 
 
        x = hash; 
 
        u.Password = x; 
 
        u.ConfirmPassword = x; 
 
        Utilisateur utilisateurV = log_existe("admin", u.login, u.Password); 
 
        if (utilisateurV != null) 
 
        { 
 
         return RedirectToAction("admin"); 
 
        } 
 
        else { ViewBag.ResultMessage = "verifier login et password !"; } 
 
       } 
 
      } 
 
      return View(u); 
 
     }

、これは私の第二のコントローラである:私はこの中に作成する方法には、第1のコントローラにログイン方式からユーザーのログインを送信したいですコントローラ:

using System; 
 
using System.Collections.Generic; 
 
using System.Data; 
 
using System.Data.Entity; 
 
using System.Linq; 
 
using System.Net; 
 
using System.Web; 
 
using System.Web.Mvc; 
 
using Kendo.Mvc.Extensions; 
 
using Kendo.Mvc.UI; 
 
using mvc_depences.Models; 
 
namespace mvc_depences.Controllers 
 
{ 
 
    public class ProjetController : Controller 
 
    { 
 
     private BD_GestionDepences db = new BD_GestionDepences(); 
 
     public ActionResult Index() 
 
     { 
 
      return View(); 
 
     } 
 
     //public ActionResult beforeCreate() 
 
     //{ 
 

 
     //} 
 
     public ActionResult Create() 
 
     { 
 
      ViewBag.UtilisateurID = new SelectList(db.Utilisateurs, "UtilisateurID"); 
 
      return View(); 
 
     } 
 
     [HttpPost] 
 
     [ValidateAntiForgeryToken] 
 
     public ActionResult Create([Bind(Include = "ProjetId,nomP,DateDebut,DateFinPrevue,DateFinReele,etat,Description,UtilisateurID")]Projet projet) 
 
     { 
 
      if(ModelState.IsValid) 
 
      { 
 
       db.Projets.Add(projet); 
 
       db.SaveChanges(); 
 
       return RedirectToAction("Index"); 
 
      }; 
 
      return View(projet); 
 
     } 
 
     public ActionResult Projet_Read([DataSourceRequest]DataSourceRequest request) 
 
     { 
 
      IQueryable<Projet> projets = db.Projets; 
 
      DataSourceResult result = projets.ToDataSourceResult(request, projet => new 
 
      { 
 
       ProjetId = projet.ProjetId, 
 
       nomP = projet.nomP, 
 
       DateDebut = projet.DateDebut, 
 
       DateFinPrevue = projet.DateFinPrevue, 
 
       DateFinReele = projet.DateFinReele, 
 
       etat = projet.etat, 
 
      }); 
 
      return Json(result); 
 
     } 
 
     //public ActionResult Index() 
 
     //{ 
 
     // return View(); 
 
     //} 
 
    } 
 
}

+0

どうやって他のコントローラを呼び出していますか、コントローラメソッドのシグニチャは何ですか? (いいえ、あなたは 'ViewData'を使うことはできません) –

+0

"どうやって他のコントローラを呼びますか? "私は何も呼んでいない!私はちょうどコントローラから別のintを送信する必要があります(つまり、ユーザーが私はこのユーザーが私はプロジェクトと呼ばれるオブジェクトを持っているときに、このユーザーがforiegnキーとしてそれを置くために私のログインを "ProjectController" ) – kokomoi

+0

あなたは状態に疑問を持っています_コントローラから別のものに変数を送るにはどうすればいいですか? –

答えて

1

ViewDataを使用して、コントローラから別のコントローラに変数を送信することはできません。

はちょっと男は最終的に私の問題が解決され、以下の方法

return RedirectToAction("ActionName", "ControllerName", new {variable1 = value1, variable2 = value2/*...etc*/}); 
0

を呼び出し、認証が成功すると実行することができます:D !!! 私はViewDataとViewBagのように動作しますが、コントローラ間の通信にはTempDataを使用しました

関連する問題