2012-03-15 39 views
0

嗨即時嘗試從部分視圖或控制器(mvc)在頭部放置鏈接文件,但無法找出或找到任何好的mvc 2示例,任何人都有想法?從部分視圖放置頭文件的css文件

我試圖this評論吉姆Tollan由

它呈現在身體我下面,即時通訊不知道這是什麼好?

<script type='text/javascript'> 

    $(function(){ 

     $('head').prepend('<link rel="canonical" href="/product/productname/productid"></link>'); 

    }); 

</script> 

我很感激任何幫助,我可以得到這個即時抓我的頭在這個問題上整天現在。

編輯
這是我當前的代碼(這僅僅是樣機代碼複製我的真實的情景)

索引頁

頭內容

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server"> 
    <%: ViewData["Message"] %> 
</asp:Content> 

<div id="container"> 
     empty 
</div> 

    <a href="#" class="click-me">Click me</a> 

控制器操作

public ActionResult Index() 
    { 
     ViewData["Message"] = "this is"; 
     return View(); 
    } 

[HttpPost] 
public PartialViewResult Index(string value) 
{ 
    ViewData["Message"] = "this is not"; 
    return PartialView("_homePartial"); 
} 

jQuery的

$(".click-me").click(function() { 
       $.post("/Home/Index", function (data) { $('#container').html(data); }, "html"); 

和多數民衆贊成它,其明顯的可視數據被設置在控制器後的動作,但之後其未設置,這就是爲什麼我需要一個jQuery的解決方案或辦法在頭像視圖中設置頭部內容。 });

+0

任何機會,你可以升級到ASP.Net MVC 3並開始使用Razor與@RenderSection語法? – 2012-03-15 15:17:06

+0

你連接到的stackoverflow問題中的哪個方法你嘗試過? – 2012-03-15 15:18:31

+0

@ShaneCourtrille,這將如何幫助? – 2012-03-15 15:23:02

回答

0

我又得瑟該解決方案的影響,並進行監控,但是這是我落得這樣做。我把它放在我的部分視圖中,那就是它。如果有人對這個好或壞有一些評論,請讓我知道。

$('head').append('content'); 
0

由於您使用的是Mvc 2,因此您可以使用ViewData

用適當的鏈接在部分視圖中設置ViewData。

ViewData["productlink"] = "/product/productname/productid"; 

在佈局頁有這樣的代碼裏面head部分

if(ViewData["productlink"] != null){ 
    <link rel="canonical" href="<%= ViewData["productlink"].toString() %>"></link> 
} 
+0

好吧,我有可能呈現鏈接的動作,但即時通訊使用jQuery來渲染一個部分,所以我的問題是你的例子將它放在視圖標題contentplaceholder中並在這種情況下渲染它?就像我說的即時通訊嘗試從局部視圖做到這一點 – 2012-03-15 15:28:55

+0

我試過了,它不起作用,我需要從局部視圖更新頭部。 – 2012-03-15 15:44:09

+0

你能證明你所嘗試過的嗎? – ShankarSangoli 2012-03-15 15:52:51