[英]Partial View not working propely in index view.
我正在開發MVC應用程序並使用razor語法。
在此應用程序中,我提供了評論功能。
我添加了一個局部視圖,該視圖從數據庫加載注釋/記錄。
在下圖中,我們可以看到注釋框,該注釋框稱為“雇員索引視圖的運行時”。
現在我們可以看到注釋框,我在運行時調用了局部視圖,但是問題是我只能在第一個記錄上添加注釋...在第一個記錄之后,該按鈕根本無法使用...
缺少什么? 當我們調用任何局部視圖運行時並對它進行操作時,是否有單獨的過程?
看圖片...
這是代碼...
@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>
為了進一步解釋AlbertoLeó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或jquery代碼。 這是一個花費了我很多時間的錯誤。 由ajax上傳的任何內容或任何renderpartiAl都需要調用javascript。
$('.RemarkBox').load(url, function() {
//RECALL JAVASCRIPT
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.