简体   繁体   English

重复部分视图Jquery + MVC 5

[英]Repeat partial view Jquery + MVC 5

I have a button that calls a partial view in which a table is loaded with all the categories of my system 我有一个按钮,它调用一个局部视图,在该局部视图中,表中加载了系统的所有类别

 <input type="button" value="Cargar Categorias" id="btnCargarCategorias" />

This load is made through a partial view which I charge inside a <div> 此负载是通过我在<div>内部加载的局部视图进行的

 <div id="divTabla">

    </div>

My javascript code that loads the table by clicking on the button is the following... 我的通过单击按钮加载表格的javascript代码如下:

 <script>
        $("#btnCargarCategorias").click(function () {

            $(this).prop("disabled", "disabled");

            var url = '@Url.Action("CargarCategorias","Categorias")';

            $.get(url).done(function (data) {
                $("#divTabla").append(data);
            })

            $("#btnCargarCategorias").removeAttr("disabled");
        })
    </script>

My action in the controller: 我在控制器中的动作:

 public PartialViewResult CargarCategorias()
        {
            var categorias = db.Categorias.ToList();

            return PartialView("_TablaCategorias", categorias);
        }

The problem is that when you click the button a second time, it reloads the table below the first one and so on if I keep pressing the button ... 问题是,当您第二次单击该按钮时,它将重新加载第一个按钮下方的表格,依此类推,如果我一直按下该按钮...

表重复

Expected behavior: regardless of the times you click on the button, the table is loaded in the same place 预期的行为:无论您单击按钮多少次,表都将被加载到同一位置

behavior obtained: each time the button is pressed, a new table is loaded below the previous one 获得的行为:每次按下该按钮,都会在前一个表下方加载一个新表

What do I have to do in my code to obtain the desired behavior? 为了获得所需的行为,我必须在代码中做什么? I have to occupy Ajax technology? 我必须占领Ajax技术吗? any help for me? 对我有帮助吗?

Because you did not clear you div before appending data into it. 因为您没有在将数据附加到div之前清除div。

You may do the following 您可以执行以下操作

$.get(url).done(function (data) {
    $("#divTabla").html(data);
})

Which will clear the inner body of the div, and then put the data inside. 这将清除div的内部主体,然后将数据放入其中。

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

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