2012-01-05 116 views
1
的RenderAction

我設置在我的HTML標籤的類時發送特定的查詢字符串參數,現在我做這樣的(Razor視圖母版頁):查詢字符串和

@if (HttpContext.Current.Request.QueryString.AllKeys.Contains("Foo") && HttpContext.Current.Request.QueryString["Foo"] == "Bar") { 
    //Do something when Foo=Bar (like http://server/route?Foo==Bar) 
    <html class="bar-class"> 
} 
else { 
    //Normal html tag 
    <html> 
} 

工作正常正常的請求,但不是當我打電話使用的的RenderAction頁面,就像

//Other view, the one requested by the user 
@Html.RenderAction("Index", "Route", new {Foo="Bar"}) 

一些四處尋找我已經認識到只有一個實際的HttpContext,這意味着HttpContext.Current指向第一個請求。那麼 - 如何獲取子請求的查詢字符串數據?

謝謝! /Victor

+0

您在'new {Foo =「Bar」}''中缺少一個報價。 – 2012-01-05 12:42:16

+0

謝謝Pankaj,固定。 – Victor 2012-01-09 10:01:07

回答

0

至少現在我通過使用TempData字典和使用後刪除值解決了問題,但我仍然對更好的解決方案感興趣。似乎應該有一種方式來獲得routedata通過...

/Victor

0

您可以使用string作爲您的Model,而不使用查詢字符串。

@model string 
@if (!string.IsNullOrWhiteSpace(Model) && Model == "Bar") { 
    //Do something when Foo=Bar (like http://server/route?Foo==Bar) 
    <html class="bar-class"> 
} 
else { 
    //Normal html tag 
    <html> 
} 

public ActionResult Route(string foo){ 
    return View(foo); 
} 
+0

問題是我需要這個通用 - 我將在_every_請求的佈局中使用,並且模型需要由實際視圖指定。所以我真的需要訪問原始查詢字符串(或similair)...也許過濾器可能是一個想法,但我不知道如何... – Victor 2012-01-09 10:00:22