javascript - 部分 View 在索引 View 中无法正常工作。

标签 javascript asp.net-mvc-3

我正在开发 MVC 应用程序并使用 razor 语法。

在此应用程序中,我提供评论功能。

我添加了一个部分 View ,它从数据库加载评论/记录。

在下图中,我们可以看到注释框,称为员工索引 View 的运行时。

现在我们可以看到评论框,我在运行时调用,这是部分 View ,但问题是我只能在第一条记录上添加评论...在第一条记录之后该按钮根本不起作用...

有什么东西丢失了吗? 当我们调用任何部分 View 运行时并对其进行操作时,是否有单独的过程?

看图片...

enter image description here

这是代码......

@model PagedList.IPagedList<CRMEntities.Customer>


  <link href="../../Content/Paging.css" rel="stylesheet" type="text/css" />
  <link href="../../Content/EventEntity.css" rel="stylesheet" type="text/css" />
  <script src="<%=Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")%>" type="text/javascript"></script>

<div id="ListBox">
    <div id="ListHeader">   
        All customers(@Model.TotalItemCount)
    </div>

    @foreach (var item in Model)
    {        

    <div id="ListContent">
          <span class="ContentTitleField">@Html.ActionLink(item.Name, "Details", new { id = item.Id }, new { @style="color:#1A6690;" })</span>
          @if (item.Owner != null)
          {               
            <span class="ContentSecondaryField">@Html.ActionLink(item.Owner.FullName, "Details", "Employee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>          
          }
          <span class="ContentSecondaryField">@Html.DisplayFor(modelItem => item.Address)</span>
          <span id="flagMenus">
            @Html.Action("ShowFlag", "Flagging", new { entityId=item.Id, entityType="Customer"})
          </span>
          @if (item.Opportunities.Count > 0)
          {
                        <span class="FlagOpportunity">@Html.ActionLink("opportunities(" + item.Opportunities.Count + ")", "Index", "Opportunity", new { custid = item.Id }, new { @style = "color:#fff;" })</span>
          }

           <div style="float:right;">
             @Html.Action("SetRate", "Rating", new { entityId = item.Id, rating = item.Rating, entityname = "Customer" })

           </div>
           <div id="subscribeStatus" style="float:right;">
                @Html.Action("ShowSubscribedStatus", "Subscribing", new { entityId = item.Id, entityType = "Customer" })
            </div>
          <div class="ListLinks">          
          <span class="ListEditLinks">
            <span style="float:left;">@Html.ActionLink("edit", "Edit", new { id = item.Id })</span>
            <span class="LinkSeparator"></span>            
           </span>
           <span class="ListAddLinks">
            <span style="float:left;">@Html.ActionLink("+opportunity", "Create", "Opportunity", new { custid = item.Id }, null)</span>
            <span class="LinkSeparator"></span>
            <span>@Ajax.ActionLink("+Comment", null, null, null, new { id = item.Id, @class = "addremark" })</span>                                  
          </span>

        <div class="RemarkBox"></div>

          </div>    

             <span class="CommentAdd">

             </span>

          <div class="CommentBlock">


        </div>

         <span>@Ajax.ActionLink("Add Comment", null, null, null, new { id = item.Id, @class = "addremark" })</span>                                  



    </div>    
    } 

    <div class="PagingBox">
        @Html.Action("CreateLinks", "Pager", new { hasPreviousPage = Model.HasPreviousPage, hasNextPage = Model.HasNextPage, pageNumber = Model.PageNumber, pageCount = Model.PageCount })
    </div>

</div>

<script type="text/javascript">

    $(document).ready(function () {

        $('.RemarkBox').hide();

        $('a.addremark').click(function () {


            var url="@Html.Raw(Url.Action("ShowCommentBox", "Comment", new { Id = "idValue", EntityType = "Customer" }))";

            url=url.replace("idValue",event.target.id);
            $('.RemarkBox').load(url);

            $(this).closest('div').find('div.RemarkBox').slideToggle(300);
            return false;
        });


         $("a.pagenumber").click(function () {             
            var page = 0;
            page = parseInt($(this).attr("id"));

            $.ajax({
                url: '@Url.Action("GetPagedCustomers")',
                data: { "page": page },
                success: function (data) { $("#customerlist").html(data); }
            });
            return false;
        });

    });

</script>

最佳答案

为了扩展 Alberto León 的说法,部分页面加载不会导致文档就绪事件触发,因此在添加第一个评论后,重新渲染的元素不会注册 javascript 事件处理程序。

要解决此问题,您可以将事件注册代码放入一个函数中,并从文档就绪事件和 AJAX 调用的成功处理程序中调用该函数。像这样的事情:

function AssignEventHandlers() {

    $('a.addremark').click(function () {
        ....
    });

    $("a.pagenumber").click(function () {
        var page = 0;
        page = parseInt($(this).attr("id"));

        $.ajax({
            url: '@Url.Action("GetPagedCustomers")',
            data: { "page": page },
            success: function (data) { 
                $("#customerlist").html(data);
                AssignEventHandlers();
            }
        });
        return false;
    });
}

$(document).ready(function () {

    $('.RemarkBox').hide();

    AssignEventHandlers();
}

关于javascript - 部分 View 在索引 View 中无法正常工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12524595/

相关文章:

javascript - 如果选中 "Other"复选框并获取值,则取消选中所有复选框

asp.net-mvc - 在 MVC3 中下载 Azure Blob 文件

Javascript或JQuery如何替换字符串中的所有 '+'

javascript - react native ListView 项目 ZIndex

javascript - 在 Ember.Controller 中对集合进行排序

asp.net-mvc-3 - 服务器端的 Textarea 和 StringLength 验证器将 'enter' 计为两个字符

asp.net-mvc - 如何在 Razor View 文件中使用通用语法?

c# - MVC3 项目中标准 ninject 和 ninject.web.mvc 之间的区别?

javascript - 调试位于局部 View 中的脚本

javascript - 跳转到页面部分并执行 JavaScript 函数