2012-07-07 26 views
1

我試圖環繞MVC.NET 3.返回到當前視圖後行動_Layout.cshtml

我使用_Layout.cshtml爲基礎(結構,導航)我的頭。作爲佈局的一部分,我想顯示兩個用於更改語言/本地化的鏈接。

無論查看哪個頁面,以及更改本地化後,都應顯示並點擊這些文件。我想重新載入名爲操作的視圖。因此,客戶正在查看的頁面將重新加載,並帶有新的本地化設置。

一種方法是在每個站點控制器中複製和粘貼本地化更改操作,但是沒有更簡單更優雅的方式嗎?

我試着創建一個特定的控制器來處理本地化的變化,但無法弄清楚如何將查看器返回到前一個控制器。

也許這更容易用jQuery完成?

這是_Layout文件中的DIV,它帶有語言更改按鈕。它調用當前控制器中的動作,這意味着我必須在每個站點的控制器中定義它。 (好消息是返回的觀點是總是正確的。)

<div id="top> 
    @using (Html.BeginForm()) 
    { 
     <button id="sv_flag" name="localization" title="@Resources.Global.Sv_Flag_Hover" value="sv-SE" /> 
     <button id="en_flag" name="localization" title="@Resources.Global.En_Flag_Hover" value="en-GB" /> 
    } 
</div> 

我使用一個特定的控制器,這也嘗試過,但想不到的,我怎麼能回到事後當前視圖?像這樣:

@using (Html.BeginForm("LocalizationAction", "LocalizationController")) 
... 

編輯 現在利用達林的建議,我在controlleraction值發送從佈局頁:

@using (Html.BeginForm("SetLocalization", "Localization", 
      new { returnController = @ViewContext.Controller.ValueProvider.GetValue("controller").RawValue, 
        returnAction = @ViewContext.Controller.ValueProvider.GetValue("action").RawValue })) 
... 

但我不能獲取本地化修改工作,我的控制器動作如下所示:

public ActionResult SetLocalization(string localization, string returnController, string returnAction) 
    { 
     Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localization); 
     return RedirectToAction(returnAction, returnController); 
    } 

回答

1

我發現一個完全不同的(而且更容易和更優雅)解決方案,我的問題。

我簡單地創建了一個BaseController,它包含了更改本地化的操作。

然後,我添加到該網站的所有控制器都繼承此BaseController。這爲代碼提供了一個位置,並且不需要發送任何返回參數等。

BaseController

public class BaseController : Controller 
{ 
    [HttpPost] 
    public ActionResult Index(string localization) 
    { 
     Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localization); 
     return View(); 
    } 
} 

每個站點的Controllers那麼只需要繼承它,然後自己做自己的行動:

public class ApplicationsController : BaseController 
{ 
    // 
    // GET: /Applications/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 
} 
... 
+0

幾周前我有一個非常類似的問題。基地控制器是我採取的路線。非常方便各種垃圾。 – ledgeJumper 2012-07-08 05:32:47

2

您可以將返回nUrl:

@using (Html.BeginForm("LocalizationAction", "LocalizationController", new { returnUrl = Request.Url.AbsoluteUri })) 
{ 
    ...  
} 

和裏面你LocalizationAction重定向到該網址:

public ActionResult LocalizationAction(string returnUrl) 
{ 
    ... do your localization stuff and once you are done get back: 

    return Redirect(returnUrl); 
} 

很明顯,你會盲目重定向之前做一些檢查。比如返回的參數是否爲空以及它是否屬於你的域。您可以查看默認AccountController如何驗證用戶的身份。

+0

謝謝,現在用你的想法,但更新我原來的問題,因爲我不能讓本土化改變。 – roqvist 2012-07-07 18:50:36

2

回報重定向(Request.UrlReferrer。的ToString());

public ActionResult Change(String LanguageAbbrevation) 
    { 
     if (LanguageAbbrevation != null) 
     { 
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation); 
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation); 
     } 

     HttpCookie cookie = new HttpCookie("Language"); 
     cookie.Value = LanguageAbbrevation; 
     Response.Cookies.Add(cookie); 

     return Redirect(Request.UrlReferrer.ToString()); 
    } 
相關問題