简体   繁体   English

如何在ASP.NET MVC中使用javascript编写动作链接?

[英]How to write action links using javascript in ASP.NET MVC?

I have a script that appends some rows to a table. 我有一个脚本将一些行附加到表。 One of the rows has a delete link, and for that I am using a ActionLink, however the id of the element is received via js, and this is nor working: 其中一行有一个删除链接,为此我使用的是ActionLink,但是元素的id是通过js接收的,这也不起作用:

 $("#Table").last().append('<tr><td><a href=\"<%:Html.ActionLink("Delete", "DeleteElementFromSet", new {id=%>Id<%})%>">Delete</a></td><td>'+Id+'</td></tr>');

where Id is a javascript variable that gets its value from the value of a dropdownlist. 其中Id是一个javascript变量,它从下拉列表的值中获取其值。

Is there a way to use ActionLink like this? 有没有办法像这样使用ActionLink? or do I have to write down the path manually? 还是我必须手动写下路径?

Because the id is known only at the client side you will need to construct the proper url. 因为id只在客户端知道,所以你需要构造正确的url。 This being said never mix C# and javascript. 这说不要混用C#和javascript。 Here's how you might proceed: 以下是您可以继续的方式:

Start by declaring a global variable that will hold the delete link without the id part: 首先声明一个全局变量,该变量将保存没有id部分的删除链接:

<script type="text/javascript">
    var deleteUrl = '<%: Url.Action("DeleteElementFromSet") %>';
</script>

and then in a separate javascript file: 然后在一个单独的 JavaScript文件中:

$('#Table').last().append(
    $(document.createElement('tr'))
        .append($(document.createElement('td'))
            .append($(document.createElement('a'))
                .attr('href', deleteUrl + '/' + Id)
                .text('Delete')
            )
        )
        .append($(document.createElement('td'))
            .text(Id)
        )
);

Notice that you should use Url.Action instead of Html.ActionLink because you already have the anchor manually generated. 请注意,您应该使用Url.Action而不是Html.ActionLink因为您已经手动生成了锚点。

Remark: avoid using GET verbs for deleting. 备注:避免使用GET动词进行删除。 You might have bad surprises. 你可能会有不好的意外。 Use proper verb (or at least POST) when modifying state on the server such as deleting. 修改服务器上的状态(如删除)时,请使用正确的动词(或至少POST)。

Just like you have an action link helper in MVC. 就像你在MVC中有一个动作链接助手一样。 Create a helper in JavaScript where you provide an action, controller, and id to create a link. 在JavaScript中创建一个帮助程序,您可以在其中提供操作,控制器和id以创建链接。

You have to write down the path manually. 您必须手动记下路径。 That's because the C# is evaluated at compile time, and you are writing it to the document at runtime. 那是因为C#在编译时被评估,并且您在运行时将它写入文档。

A better option is to separate HTML and Javascript completely. 更好的选择是完全分离HTML和Javascript。 Put your HTML in a hidden div in your view, then copy its contents to wherever you want using javascript. 将您的HTML放在视图中的隐藏div中,然后使用javascript将其内容复制到您想要的任何位置。 This way you will still be able to use C# to create the action links. 这样您仍然可以使用C#创建操作链接。

Separating HTML from Javascript is also improves code readability, separates concerns better and expands syntax highlighting. 从Javascript中分离HTML还可以提高代码可读性,更好地分离关注点并扩展语法突出显示。 In other words, it's a good practice. 换句话说,这是一个很好的做法。

Refactor it to make it clearer. 重构它以使其更清晰。 Literally, the equivalent is: 从字面上看,相当于:

var deleteUrl = '<%:Url.ActionLink("Delete", "DeleteElementFromSet", new {id=%>Id<%})%>';
$("#Table").last()
   .append('<tr><td><a href=\"' + deleteUrl + '">Delete</a></td><td>'+Id+'</td></tr>');

First thing you can note in there is that you open it with \\" and you close it with " . 你可以注意到的第一件事就是用\\"打开它\\"并用"它关闭它"

I suggest you do the above exercise until the script is clear and the confusing syntax isn't at play. 我建议你进行上述练习,直到脚本清晰,并且令人困惑的语法不起作用。

Above said, in deleteUrl you are trying to use a client side value on the server . 上面说,在deleteUrl中, 您尝试在服务器上使用客户端值 Remember <%: will be called during the server side render of the view, so an Id you set on the client side doesn't come into play at all in there. 记住<%:将在服务器端渲染视图期间调用,因此您在客户端设置的ID根本不会发挥作用。

An option is to use a place holder, like : 一个选项是使用占位符,如

var deleteUrl = '<%:Url.ActionLink("Delete", "DeleteElementFromSet", new {id=%>Id<%})%>';
deleteUrl = deleteUrl.replace("##id##", Id);
$("#Table").last()
   .append('<tr><td><a href=\"' + deleteUrl + '">Delete</a></td><td>'+Id+'</td></tr>');

how about this: 这个怎么样:

var row = $("#Table").last().append('<tr><td><%:Html.ActionLink("Delete", "DeleteElementFromSet")%>"></td><td>' + Id + '</td></tr>');
row.find('a').attr(Id);

This will add the row, then find the link and add the id attribute. 这将添加行,然后找到链接并添加id属性。

In your code it looked like there was an extra 'a' tag that I removed. 在您的代码中,看起来我删除了一个额外的'a'标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM