我在UI中有表格數據。當我點擊行中的詳細信息鏈接時,我想在同一頁面的div中顯示數據。當我點擊任何行的細節鏈接時,JQuery函數並沒有被解僱。.NET MVC JQuery函數沒有在表格行單擊事件中被觸發
下面是我的代碼:
模型視圖類:
public class ItemViewModel
{
public Item item { get; set; }
public IEnumerable<Item> items { get; set; }
}
UI代碼:
@model Medhub.Models.ItemViewModel
@{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>View</h2>
<p>
@Html.ActionLink("Create New", "CreateEdit", new { controller = "Item" }, new { @class = "btn btn-primary" })
</p>
<table align="center" height="10%" width="90%">
<tr>
<td>
<div id="Items">
<table style="vertical-align:top; height:200px" width="100%">
<tr style="height:20px">
<th>
@Html.DisplayName("Name")
</th>
<th>
@Html.DisplayName("Description")
</th>
<th>
@Html.DisplayName("Item Type")
</th>
</tr>
@if (Model != null)
{
foreach (var item in Model.items)
{
<tr style="height:15px">
<td>
@Html.HiddenFor(model => item.ItemId)
@Html.DisplayFor(model => item.Name)
</td>
<td>
@Html.DisplayFor(model => item.Description)
</td>
<td>
@Html.DisplayFor(model => item.Type)
</td>
<td>
@Html.ActionLink("Edit", "CreateEdit", new { id = item.ItemId }) |
@Html.ActionLink("Details", "Item", new { id = item.ItemId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ItemId })
</td>
</tr>
}
}
</table>
</div>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div id="ItemDetails">
@if (Model.item != null)
{
Html.RenderPartial("Details", Model.item);
}
</div>
</td>
</tr>
</table>
<script type="text/javascript">
$(function() {
$("div.Items a").click(function (e) {
//e.preventDefault();
var url = this.ref;
$("#ItemDetails").load(url);
});
});
</script>
控制器代碼:
List<Item> items = new List<Item>();
// GET: Item
public ActionResult Item(int id = 0)
{
ItemViewModel itemVM = new ItemViewModel();
itemVM.items = GetItems();
if (id > 0)
itemVM.item = itemVM.items.FirstOrDefault(u => u.ItemId == id);
return View(itemVM);
}
任何線索?
你定義'div.Items了'選擇,這意味着用''類和Items''div'標籤一個''標籤裏面,但我找不到任何具有類'Items'的div元素。嘗試將選擇器更改爲'$('div [id^=「Items」] a')'。 –
謝謝。會嘗試讓你知道。 –