在 Razor 的 Javascript 中嵌入 Html.ActionLink [英] Embed Html.ActionLink in Javascript in Razor

查看:29
本文介绍了在 Razor 的 Javascript 中嵌入 Html.ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以在 MVC3 的 Javascript 中嵌入 @Html,但我无法使以下内容正常工作,并且不确定这是否可行.

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 DataTable,我有一个 AJAX 调用来创建我的新行,然后我使用数据表 API 以编程方式添加它.这一切正常,但随后我想将我的 Edit ActionLink 放到行上,它只显示文本Edit",而不是链接.

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;
}

我暂时硬编码了 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 })';

这将生成整个 标签.您也可以做的只是返回网址:

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

这篇关于在 Razor 的 Javascript 中嵌入 Html.ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆