我有一个徒劳的问题试图解决。首先,我有一个页面,向用户提供一些选择以执行数据库中数据的搜索。这是通过AJAX调用和页面上空白DIV的部分视图实现的。在此局部视图的内部,我试图在项目列表ID的列中添加DIV,并将其设置为display:none。单击ID后,我想通过AJAX发送请求以提取该ID的详细信息,然后使用另一个局部视图加载动态创建的DIV。这是页面示例:
//div with table of fields to enter selections for search
<div>
<input type="button" value="search" onclick="Search()" />
<div>
<div id="searchResults"></div>
<script type="text/javascript">
function GetData() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetTours", "Tours")',
data: $(".searchModel").serializeArray(),
success: function(data) {
$("#searchResults").html(data);
$("#searchResults").show();
}
});
}
</script>
这工作正常,我对此没有任何问题。在搜索结果的局部视图中,我正在这样做:
<div>
<table>
<tr>
<th>ID</th>
<th>Date</th>
// more headers
<tr>
@if (Model != null)
{
foreach (var tour in Model.Tours)
{
<tr>
<td>
<a onclick="showTour(@tour.id)">@tour.id</a>
</td>
<td>
@tour.date
</td>
<tr>
<tr>
<td colspan="6">
<div id="string.Format("div{0}, tour.id)"style="display:none"></div>
</td>
</tr>
}
</table>
</div>
点击事件的JQ为:
var lastId = 0;
function showTour(id) {
if (lastId === id) {
$("#div" + lastId).hide();
} else {
lastId = id;
$.ajax({
type: 'GET',
url: '@Url.Action("Selected", "Tours")',
data: ('tourId' : lastId },
success: function (data) {
$("#div" + lastId).html(data);
$("#div" + lastId).show();
}
});
}
当我调试时,我可以看到所有请求都到达了控制器,没问题,部分视图正在返回到页面。AJAX调用成功后,我可以在“数据”元素中看到整个局部视图,但DIV从未打开过。我希望它能在线下打开,但不会。我在这里做错了什么?
这似乎是不可能实现的。但是,我能够将整个模型(包括所有列表)发送到该页面,并在每个表行下构造一个展开/折叠行以显示详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句