2

I know that it's possible to embed @Html in Javascript in MVC3, but I can't get the following to work and not sure if this is possible yet.

Using jQuery DataTable, I have an AJAX call to create my new row, then I programatically add this using the datatable API. This all works, but then I want to put my Edit ActionLink onto the row and it only shows up with the text "Edit", not the link.

Of course I could do this manually, just wondering if there is a better option.

e.g.

 tablePallets.fnAddData([ GetPalletActionLinks(), etc...


    function GetPalletActionLinks() {
        var result = @Html.ActionLink("Edit", "EditPallet", new { id = 1 });
        return result;
}

I've hard coded ID = 1 for the moment, but I can easily get this for the newly created row.

Thanks Duncan

Duncan
  • 10,218
  • 14
  • 64
  • 96

1 Answers1

9

I think it's a simple as adding quotes around the link:

var result = '@Html.ActionLink("Edit", "EditPallet", new { id = 1 })';

This will generate the whole <a> tag. What you could do as well is just return the url:

var result  = '@Url.Action("EditPallet", new { id = 1 })';

and the embed it in an existing anchor using jQuery:

<!-- let's imagine this already exists -->
<a href="#" id="dynamicLink">Edit</a>

// result is ovbiously what the other function returns
$("#dynamicLink").attr("href", result);
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
  • 2
    You're right.... I thought that if I put quotes around it would be interpreted as a string literal.... still new to MVC3 and it is still surprising me what it can do. Can't see me ever doing another WebForms app! – Duncan Apr 29 '11 at 09:23
  • 1
    @Duncan - I know what you mean, same here =) – Sergi Papaseit Apr 29 '11 at 09:41