我做了一個MVC應用程序,我試圖爲用戶點擊「細節」按鈕時創建一個局部視圖。我目前有一個局部視圖用於「刪除」按鈕。當我使用斷點逐句通過我的代碼時,它將我帶到了我的任務控制器,並進入了我的PartialViewResult方法,但是不再進一步。當我點擊「詳情」按鈕時,什麼也沒有發生。不知道這裏缺少什麼。MVC局部視圖使用ajax
Index.cshtml
<span class="btn btn-success btn-sm" onclick="showTask('@item.TaskId')">Details</span>
<div id="Detail"></div>
<Script>
function showTask(showTaskID) {
$.ajax({
url: '@Url.Action("ShowTaskByID")',
data: { id: showTaskID },
success: function (data) {
$('#Detail').hide();
$('#Detail').html(data);
$('#Detail').animate({
opacity: 1,
left: "+=50",
height: "toggle"
}, 3000, function() {
// Animation complete.
});
$('#Edit').hide();
$('#Delete').hide();
},
error: function (data) { $('#Details').html('<h3>Error</h3>'); }
});
}
</script>
_ShowTask
@model IEnumerable<WebApplication4.Models.Task>
<div class="panel panel-info">
<div class="panel-heading" style="font-size:20px">
<h2>List of Actors</h2>
</div>
<p>
@Html.ActionLink("Create New Task", "CreateTask", null, new { @class = "btn btn-sm btn-primary" })
</p>
@if (Model.Any())
{
<table class="table table-condensed table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().TaskName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().StartDate)
</th>
<th>
@Html.DisplayNameFor(model => model.First().FinishDate)
</th>
<th></th>
</tr>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TaskName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.FinishDate)
</td>
<td>
@Html.ActionLink("Edit", "EditTask", new { id = item.TaskId }, new { @class = "btn btn-info btn-xs" })
@*<a onclick="showEditActor('@item.MovieID','@item.ActorID','@item.ActorName','@item.age')" class="btn btn-xs btn-info">Edit</a>*@
@Html.ActionLink("Delete", "DeleteTask", new { id = item.TaskId }, new { @class = "btn btn-danger btn-xs" })
</td>
</tr>
}
} @* closing of if *@
</table>
}
else
{
<div><strong>No Actors in Movie</strong></div>
}
</div>
任務控制器
public ActionResult Details(int id)
{
var q = db.Tasks.Find(id);
if (q == null)
{
}
return View(q);
}
public PartialViewResult ShowTaskByID(int id)
{
return PartialView("_ShowTask", db.Tasks.Find(id).TaskName);
}
運行菲德勒在加載部分,看看什麼是返回。聽起來像一個客戶端問題 – Tim