2012-04-19 73 views
2

_ViewStart.cshtml的Html不正確的渲染並沒有Viewbag.Title使用Ajax.ActionLinks

@{ 
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";  
} 

_Layout.cshtml設置

... 
</head> 
<body> 
    <div id="NavigationPanel"> 
     @{ AjaxOptions options = new AjaxOptions 
     { 
      InsertionMode = InsertionMode.Replace, 
      UpdateTargetId = "ContentPanel", 
      OnBegin = "OnBeginAnimateContentPanel", 
      OnComplete = "OnCompleteAnimateContentPanel", 
     };} 
     <div> 
      <p>@Ajax.ActionLink("Users", "Index", "User", options)</p> 
      <p>@Ajax.ActionLink("Groups", "Index", "Group", options)</p> 
      <p>@Ajax.ActionLink("Permissions", "Index", "Permission", options)</p> 
     </div> 
    </div> 
    <div id="ContentPanel"> 
     @RenderBody() 
    </div> 
</body> 
</html> 

的global.asax.cs

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Main", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

MainController.Index行動:

public ActionResult Index() 
{ 
    return View(); 
} 

主視圖Index.cshtml

@{ 
    ViewBag.Title = "Test-Index"; 
} 

this is the Index View of the MainController 

索引視圖+行動爲用戶/組/許可控制器就像是MainController的指數。標題設置爲ViewBag和一些小測試文本。

這裏有一件事出錯了。

當我點擊其中一個Ajax.ActionLInks時,雖然View在#ContentPanel中正確呈現,但每個View(用戶/組/權限)的標題都是不可見的,但是當我檢查html源代碼時:

ContentPanel中的內容是索引視圖中的內容?

這是爲什麼,我怎樣才能爲每個Ajax.ActionLink設置我的View標題?

<body> 
    <div id="NavigationPanel"> 
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/User">Users</a></p> 
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/Group">Groups</a></p> 
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/Permission">Permissions</a></p> 
    </div> 
    <div id="ContentPanel"> 
    this is the text within the Index View of the MainController 
    </div> 
</body> 

回答

1

當你查看源代碼,你看那個初始加載源;通過AJAX加載的內容不會出現。但是,如果您使用DOM檢查器,它將顯示動態加載的內容。至少這是它在Chrome中的工作方式;我想其他瀏覽器是一樣的。

檢查DOM檢查器,查看代碼是否符合您的期望(右鍵單擊「Chrome和Firefox上的Inspect元素」)。

至於設置標題,那不能用AJAX完成,至少不是沒有一些Javascript幫助。在部分視圖中,請嘗試使用以下Javascript:

<script> 
    document.title = "My New Title Here"; 
</script> 
+0

這將是答案的50%。爲什麼當我點擊一個Ajax.ActionLink時,我的標題沒有顯示,我該如何解決? – Pascal 2012-04-19 20:12:38

+0

我更新了答案......我希望它有幫助。 – 2012-04-19 20:24:59

+0

我會將您的答案標記爲獨立於此進一步問題的解決方案,也許您也知道答案:)我的MainController Index操作實際上毫無價值。它只是一個起點......是不是可以直接使用_Layout.cshtml文件開始? – Pascal 2012-04-19 20:48:35