我有一個視圖,它有一些jQuery將視圖中的部分視圖(通過按鈕單擊)加載到div中。這工作沒有問題。然而,在部分視圖中,我有一個非常相似的jQuery,它會在第一個局部視圖中將另一個局部視圖加載到div中,但這不起作用,它幾乎看起來像第一個局部視圖中的jQuery不存在加載。JQuery在部分視圖中沒有被調用
我嘗試過尋找解決方案,但是我還沒有設法找到答案。我也在@Ajax.ActionLink
中重新創建了jQuery函數,但是我試圖避免Microsoft傭工,因爲我正在嘗試學習jQuery。
這裏是包含了jQuery的第一部分視圖似乎並不工作,同時也包含了不工作的@Ajax.ActionLink
:
@model MyProject.ViewModels.AddressIndexViewModel
<script>
$(".viewContacts").click(function() {
$.ajax({
url: '@Url.Action("CustomerAddressContacts", "Customer")',
type: 'POST',
data: { addressID: $(this).attr('data-addressid') },
cache: false,
success: function (result) {
$("#customerAddressContactsPartial-" + $(this).attr('data-addressid'))
.html(result);
},
error: function() {
alert("error");
}
});
return false;
});
</script>
<table class="customers" style="width: 100%">
<tr>
<th style="width: 25%">
Name
</th>
<th style="width: 25%">
Actions
</th>
</tr>
</table>
@foreach (Address item in Model.Addresses)
{
<table style="width: 100%; border-top: none">
<tr id="[email protected]">
<td style="width: 25%; border-top: none">
@Html.DisplayFor(modelItem => item.Name)
</td>
<td style="width: 25%; border-top: none">
<a href="#" class="viewContacts standardbutton" data-addressid="@item.AddressID">ContactsJQ</a>
@Ajax.ActionLink("Contacts", "CustomerAddressContacts", "Customer",
new { addressID = item.AddressID },
new AjaxOptions { UpdateTargetId = "customerAddressContactsPartial-" + @item.AddressID, HttpMethod = "POST" },
new { @class = "standardbutton"})
</td>
</tr>
</table>
<div id="[email protected]"></div>
}
如果有人能說明什麼我在這裏做錯了,以及如何要解決它,那麼我將非常感激。
非常感謝。
非常感謝。這非常有效,但ajax成功中的'.closest()。find()'不起作用。我用只是一個div來替換它來測試,並且工作。我已完全按照您對視圖所做的更改,所以我不確定是哪裏出了問題。 – XN16
忽略我最近的評論,它似乎現在工作!只是一件小事; ActionLink中的數據應該是'new {addressid = item.AddressID}',否則它是完美的! – XN16
你確實是對的,它應該是'new {addressid = item.AddressID}'。我也從AJAX請求中刪除了'data:{addressid:$(this).data(addressid)}',因爲這是url的一部分,不需要。我還將HTTP動詞更改爲GET,因爲這似乎是用於從服務器獲取數據的更正確的動詞。在實際修改服務器上的某個狀態時使用POST。 –