2017-07-10 138 views
0

當我註冊一個用戶時,它將重定向到管理控制器(用戶配置文件所在的位置)。將消息從一個控制器發送到另一個控制器

return RedirectToAction("Index", "Manage", new { id = user.Id });

,並在管理控制器

[Route("")] 
     [Route("{id}")] 
     public ActionResult Index(string id) 
     { 

      if (id == null) 
      { 
       id = User.Identity.GetUserId(); 
      } 


      var user = UserManager.FindById(id); 

      return View(user); 
     } 

我需要發送「NEWUSER」的字符串信息,所以可以顯示一個模式消息的第一次用戶使帳戶和是重定向到他們的個人資料。

我的問題是如何?我試圖發送一個字符串參數是這樣的:

return RedirectToAction("Index", "Manage", new { id = user.Id, msg = "newUser" });

,改變指數的構造,以Index(string id, string msg)但由於某種原因這兩個ID和味精獲得用戶id的值。

任何人都知道這個解決方案嗎?

管理/索引

public ActionResult Index(string id) 
     { 
      if (id == null) 
      { 
       id = User.Identity.GetUserId(); 
      } 


      var user = UserManager.FindById(id); 

      return View(user); 
     } 
+0

我刪除它,同樣的事情發生。但是,由於某些原因重定向時,URL在「&msg = newUser」中結束? – crystyxn

+0

再次....問題的消息結束了採取ID的價值,但如何?這似乎是如此隨機 – crystyxn

回答

1

您可以使用TempData將數據從一個控制器傳遞給另一個控制器。

例子:

public ActionResult Index() 
{ 
    var model = new Review() 
     { 
      Body = "Start", 
      Rating=5 
     }; 
    TempData["ModelName"] = model; 
    return RedirectToAction("About"); 
} 


public ActionResult About() 
{  
    var model= TempData["ModelName"];  
    return View(model); 
} 

請參閱以下鏈接獲取更多信息:https://www.codeproject.com/articles/476967/what-is-viewdata-viewbag-and-tempdata-mvc-option

+0

有人已經提出這個問題,爲什麼它不起作用? – crystyxn

+0

無論我怎麼試用這個TempData似乎都沒有做任何事情... – crystyxn

+0

向我發送管理方法的代碼 – Dilip

0

您的代碼語法幾乎是正確的,除了你缺少味精=」 NEWUSER

+0

對不起,當我輸入這個我忘了,但沒有在代碼 – crystyxn

+0

然後問題是與路由。您必須在ActionResult中添加兩個參數。 –

+0

我刪除了路線,但它的結果相同 – crystyxn

0

如果你真的想那麼兩個參數裏面App_Start >> Routeconfig.cs文件,你可以提

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Manage", action = "Index", id = UrlParameter.Optional,msg = UrlParameter.Optional } 
      ); 

而且你ManageController內修改你的行動像低於

public class ManageController : Controller 
{ 
    public ActionResult Index(string id, string msg) 
    { 

     if (id == null) 
     { 
      id = User.Identity.GetUserId(); 
     } 


     var user = UserManager.FindById(id); 

     return View(user); 
    } 
} 
+0

我得到同樣的問題....由於某種原因,味精與id具有相同的值 – crystyxn

相關問題