【问题标题】:Dynamically added partial view to existing DIV on button click in JQUERY and MVC在 JQUERY 和 MVC 中单击按钮时将部分视图动态添加到现有 DIV
【发布时间】:2014-01-13 16:15:01
【问题描述】:

我正在向现有 div 添加部分视图以添加更多重复的用户字段。我就是这样做的:

     <div class="userDiv"> Existing HTML goes here </div>     

AND 锚链接以添加另一个用户

    @Html.ActionLink("Add another User", "AddUser", null, new { id = "addAnotherUserLink" })  


$("#addAnotherUserLink").click(function () {
                    $.ajax({
                        url: this.href,
                        cache: false,
                        success: function (html) { $(".userDiv").append(" <div class='userPartialDiv'><a href='#' class='deleteRow'>Delete</a>" + html + "</div>"); }
                    });
                    return false;
                });

当你点击删除时,它会删除附加的局部视图。

    $(document).on("click", ".deleteRow", function () {
                   $(this).closest(".userPartialDiv").remove();
                   return false;
               });

我的问题是如何给它一个数字。 假设我在添加另一个用户按钮上添加或单击了四次,它将向现有 DIV 添加四个局部视图。但我需要给他们一个像 user1、user2、user3、user4 这样的数字。但是当我删除其中一个让我们说第二个时,它应该自动重新排序给 user1,user2,user3,而不是 user1,user3,user4。

还请帮助我如何将这些数据添加到数据库中。我的意思是如何通过循环添加的部分视图的数量将数据从部分视图读取到控制器中。

感谢您的帮助。

谢谢

【问题讨论】:

    标签: jquery asp.net-mvc model-view-controller


    【解决方案1】:

    如果您在“删除”时愿意发布,请使用数据模型并在删除时重放剩余的部分视图,并在剩余的用户条目上附加 for 循环计数器。

    【讨论】:

    • 不,我必须在前端使用 jquery 动态进行。
    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多