0

Hi im trying to place a link file in the head from a partial view or a controller (mvc) but cant figure out or find any good mvc 2 example, anybody have an idea?

I tried this Comment made by Jim Tollan

It renders the following for me in the body, im not sure if this is any good?

<script type='text/javascript'>

    $(function(){

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

    });

</script>

I would appreciate any help i could get on this im scratching my head on this issue all day long now.

EDIT
this is my current code(this is only mockup code to replicate my real scenario)

on the index page

head content

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

body

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

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

controller actions

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");

and thats it, its obvious the viewdata is set in the post action in the controller but its not set after that, thats why i need a jquery solution or a way to set the head content in the parialview. });

Community
  • 1
  • 1
Dejan.S
  • 18,571
  • 22
  • 69
  • 112

2 Answers2

0

Since you are using Mvc 2 you can make use of ViewData.

Set the ViewData in the partial view with appropriate link.

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

In the layout page have this code inside head section

if(ViewData["productlink"] != null){
     <link rel="canonical" href="<%= ViewData["productlink"].toString() %>"></link>
}
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • Ok i got the possiblity to render the link in the action but im using jquery to render a partial, so my question is will your example place it in the views header contentplaceholder and render it in that case? like i said im trying to do this from a partial view – Dejan.S Mar 15 '12 at 15:28
  • I tried it and it does not work, i need to update the head from the partial view. – Dejan.S Mar 15 '12 at 15:44
0

i yet have to se the effects of this solution and monitor it but this is what i ended up doing. i placed this in my partial view and thats it. if anybody got some comment on this good or bad, please let me know.

$('head').append('content');
Dejan.S
  • 18,571
  • 22
  • 69
  • 112