【问题标题】:button doesnt work after load partialview with AJAX使用 AJAX 加载局部视图后按钮不起作用
【发布时间】:2014-03-21 14:36:35
【问题描述】:

我的页面有两个按钮 Next 和 Previos。此按钮在 PartialView 中加载表格。 我的问题是当我第二次按下按钮时,它不起作用。

这里是代码

观点:

<div id="Centros">
                <div class="divPantalla2">
                    <table cellspacing="0" class="table table-hover table-responsive table-bordered">
                        <tr>
                            <th class="thTablas">
                                Nombre
                            </th>
                            <th class="thTablas">
                                Nro- Establecimiento
                            </th>
                            <th class="thTablas">
                                Departamento
                            </th>
                            <th class="thTablas">
                                Localidad
                            </th>
                            <th class="thTablas">
                            </th>
                        </tr>
                        @if (ViewBag.OnePageOfCEProyecto != null)
                        {



                            foreach (ANEP.Models.CentroEducativoModel item in ViewBag.OnePageOfCEProyecto)
                            {

                            <tr>
                                <td>
                                    @item.Nombre
                                </td>
                                <td>
                                    @item.NroEstablecimiento
                                </td>
                                <td>
                                    @if (item.dDepartamento != null)
                                    {
                                        @item.dDepartamento.Descripcion;
                                    }
                                </td>
                                <td>
                                    @if (item.dLocalidad != null)
                                    {
                                        @item.dLocalidad.NomLocalidad;
                                    }
                                </td>
                                <td>
                                    <button  type="submit" value="Eliminar+@item.CEID"  name="Command" class="btn_eliminar">
                                    </button>
                                </td>
                            </tr>         
                            }

                        }
                    </table>
                </div>
                <div id="botones">
                    @if (ViewBag.OnePageOfCEProyecto != null)
                    {
                        <div align="center">
                            <div align="center">
                                @if (Session["PROYECTO_PAGINA_ACTUAL"] != null && int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString()) > 1)
                                {

                                    <button type="button" id="Primera" value="Primera1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8"><<</font></button>

                                    <button type="button" id="Anterior" value="Anterior1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8"><</font></button>
                                }
                                <font color="#0C58A8">@int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString())</font>
                                @if (ViewBag.OnePageOfCEProyecto.Count == 8)
                                {

                                    <button type="button" id="Siguiente" value="Siguiente1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8">></font></button>

                                    <button type="button" id="Ultima" value="Ultima1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8">>></font></button>
                                }
                            </div>
                        </div>
                    }
                </div>
            </div>

部分视图:

                <div class="divPantalla2">
                    <table cellspacing="0" class="table table-hover table-responsive     table-bordered">
                        <tr>
                            <th class="thTablas">
                                Nombre
                            </th>
                            <th class="thTablas">
                                Nro- Establecimiento
                            </th>
                            <th class="thTablas">
                                Departamento
                            </th>
                            <th class="thTablas">
                                Localidad
                            </th>
                            <th class="thTablas">
                            </th>
                        </tr>
                        @if (ViewBag.OnePageOfCEProyecto != null)
                        {



                            foreach (ANEP.Models.CentroEducativoModel item in ViewBag.OnePageOfCEProyecto)
                            {

                            <tr>
                                <td>
                                    @item.Nombre
                                </td>
                                <td>
                                    @item.NroEstablecimiento
                                </td>
                                <td>
                                    @if (item.dDepartamento != null)
                                    {
                                        @item.dDepartamento.Descripcion;
                                    }
                                </td>
                                <td>
                                    @if (item.dLocalidad != null)
                                    {
                                        @item.dLocalidad.NomLocalidad;
                                    }
                                </td>
                                <td>
                                    <button  type="submit" value="Eliminar+@item.CEID"  name="Command" class="btn_eliminar">
                                    </button>
                                </td>
                            </tr>         
                            }

                        }
                    </table>
                </div>
                <div id="botones">
                    @if (ViewBag.OnePageOfCEProyecto != null)
                    {
                        <div align="center">
                            <div align="center">
                                @if (Session["PROYECTO_PAGINA_ACTUAL"] != null && int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString()) > 1)
                                {

                                    <button type="button" id="Primera" value="Primera1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8"><<</font></button>

                                    <button type="button" id="Anterior" value="Anterior1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8"><</font></button>
                                }
                                <font color="#0C58A8">@int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString())</font>
                                @if (ViewBag.OnePageOfCEProyecto.Count == 8)
                                {

                                    <button type="button" id="Siguiente" value="Siguiente1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8">></font></button>

                                    <button type="button" id="Ultima" value="Ultima1" name="Command" class="btn btn-primary">
                                        <font color="#0C58A8">>></font></button>
                                }
                            </div>
                        </div>
                    }
                </div>

AJAX 功能(仅用于按钮 next):

$(document).ready(function () {
        $("#Siguiente").on("click", function () {
            var accion = $('#Siguiente').val();
            $.ajax({
                url: "/Proyecto/TablaCentros",
                type: "GET",
                cache: false,
                data: { command: accion }
            })
        .done(function (partialViewResult) {
            $("#Centros").html(partialViewResult);
        });
        });
    });

还有控制器:

public ActionResult TablaCentros(string command, Proyecto p)
    {
        if (command != null)
        {
            if (command.Contains("Siguiente1"))
            {
                if (Session["REGISTRO_PROYECTO"] != null)
            {
                Proyecto model = p;
                int page = (int)Session["PROYECTO_PAGINA_ACTUAL"];
                page++;
                Session["PROYECTO_PAGINA_ACTUAL"] = page;
                List<CentroEducativoModel> lista =   (List<CentroEducativoModel>)Session["PROYECTO_CENTRO_EDUCATIVOS"];
                var onePageOfCEProyecto = lista.ToPagedList(page, 8); // will only contain 15 products max because of the pageSize
                ViewBag.OnePageOfCEProyecto = onePageOfCEProyecto;
                cargarEstadoProyecto();
                cargarCentroEducativos();
                cargarClaseProyecto();
                cargarTipoProyecto();
                return PartialView("_TablaCentros", model);
            }
            }
        }
        return PartialView("_TablaCentros");
    }

对不起我的英语,

谢谢!!!

【问题讨论】:

  • 看起来你的观点和你的局部观点是一样的。是吗?
  • 是的,因为我的页面第一次加载带有一些元素的表格,但是当您按下一步时,页面会加载带有不同数据的部分视图
  • 如果是这种情况,那么我建议您将部分内容添加到您的页面中。这样一来,您只需将代码放在一个地方,避免重复。

标签: html ajax asp.net-mvc asp.net-mvc-4 partial-views


【解决方案1】:

看起来您的按钮是局部视图的一部分。当您最初加载您的视图(没有 ajax)时,您的按钮绑定到您的点击处理程序,一切都很好。当您单击该按钮时,您的处理程序将触发,发出请求,然后将 #Centros 内容替换为响应 - 但是您的绑定按钮是被替换内容的一部分,并且您的“新”按钮未绑定。

在完成 ajax 请求并加载结果后,您需要将处理程序重新绑定到按钮。您有几个选择——您可以将脚本移动到局部视图,以便在加载后触发,您可以在 done() 处理程序中调用绑定代码,或者您可以将现有代码的选择器更改为 @987654321 @。

我将使用类似于此的委托:

$("#Centros").on("click", "#Siguiente", function() { ... })

这实质上意味着,只要在#Centros 中对 ID 为 #Siguiente 的元素发生点击事件,就会处理该事件。

【讨论】:

    【解决方案2】:

    我希望任何答案对您有所帮助。我在控制器中的 Action 签名中注意到它应该接收两个参数 commandProyecto 但在您的 ajax 函数上您只需传递 command:accion 所以您的 Action 不会收到对象 Proyecto

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 2015-03-25
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多