简体   繁体   English

使用jquery加载和插入ASP.NET局部视图

[英]load and insert an ASP.NET partial view using jquery

I have a partial view of a row for the table. 我对该表的行有局部视图。 Each row in the table describes an element. 表中的每一行都描述了一个元素。 An element can have child elements, but if it is a child element it cannot have it's own children. 元素可以包含子元素,但如果它是子元素,则它不能拥有自己的子元素。 In the table, there is a row for each parent element, and a list of all of it's child elements. 在表中,每个父元素都有一行,以及所有子元素的列表。 I am working on having a link next to the child elements that they can click, and it will no longer be a child. 我正在努力在他们可以点击的子元素旁边有一个链接,它将不再是一个孩子。 So it needs to be removed from the list, and a row added to the table under the parent element you just took it out of. 因此需要从列表中删除它,并在您刚刚将其取出的父元素下添加到表中。 I have the following javascript funciton that correctly updates the element in the database to no longer have a parent and removes it from the list. 我有以下javascript函数,正确更新数据库中的元素,不再有父级,并将其从列表中删除。

    function removeElementFromParent(id, parentid) {
        if (confirm("Are you sure you want to remove this element from the group?")) {
            $.post('@Url.Content("~/Pack/RemoveElementFromParent")', { "elementid": id });
            $('#' + id).remove();
        }
    }

to create the row and insert it, I tried to do: 创建行并插入它,我试着做:

$('@Url.Action("~/Pack/ElementRow", new { elementid = id })').insertAfter('#' + parentid);

but that won't work since you cannot use the javascript variable id within @Url.Action(...). 但这不起作用,因为你不能在@ Url.Action(...)中使用javascript变量id。 I have used the $('#id').load(@Url.Action(...)); 我使用了$('#id').load(@Url.Action(...)); to load data as it is requested. 按要求加载数据。 I tried to do something similar, but since we don't have a defined place to load it, that wasn't going to work in this case. 我尝试做类似的事情,但由于我们没有一个定义的地方来加载它,在这种情况下这不会起作用。 Also, you still wouldn't be able to use javascript variables. 此外,您仍然无法使用javascript变量。

if there is a better way to do this, some help would be greatly appreciated 如果有更好的方法来做到这一点,将非常感谢一些帮助


EDIT 编辑

Here is my solution: 这是我的解决方案:

$('<tr id="' + id + '"></tr>').insertAfter('#' + parentid);
$('#' + id).load('@Url.Action("ElementRow")' + '?elementid=' + id);

If you want to use the javascript variable with Url.Action than you should to write something like this 如果你想使用url.Action的javascript变量,你应该写这样的东西

$('@Url.Action("~/Pack/ElementRow")' + "?elementid=" + id).insertAfter('#' + parentid);

Hope this will fix you problem. 希望这能解决你的问题。

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

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