嵌入Html.ActionLink在Javascript中剃刀 [英] Embed Html.ActionLink in Javascript in Razor
问题描述
我知道,它可以嵌入@Html在Javascript中MVC3,但我不能让下面的工作,不知道这是否可能呢。
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.
使用jQuery数据表,我有一个AJAX调用创建我的新行,然后我编程使用DataTable API添加此。这一切工作,但我希望把我编辑的ActionLink到该行,并只显示文本编辑,而不是链接。
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.
例如
tablePallets.fnAddData([ GetPalletActionLinks(), etc...
function GetPalletActionLinks() {
var result = @Html.ActionLink("Edit", "EditPallet", new { id = 1 });
return result;
}
我已经很难$ C $光盘ID = 1的时刻,但我可以很容易地得到这个新创建的行。
I've hard coded ID = 1 for the moment, but I can easily get this for the newly created row.
谢谢
邓肯
推荐答案
我认为这是一个简单的围绕链接加上引号:
I think it's a simple as adding quotes around the link:
var result = '@Html.ActionLink("Edit", "EditPallet", new { id = 1 })';
这将产生全< A>
标记。你可以做的也只是返回的网址:
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 })';
和使用jQuery嵌入它在现有的锚:
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);
这篇关于嵌入Html.ActionLink在Javascript中剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!