我已经创建了一个MVC应用程序,并且我正在尝试为用户单击“详细信息”按钮创建部分视图。我现在有一个部分视图用于我的“删除”按钮。当我使用断点逐步执行我的代码时,它将我带到了我的任务控制器并进入我的PartialViewResult方法,但后来没有进一步。当我点击“详细信息”按钮时没有任何反应。不知道这里缺少什么。
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);
}
答案 0 :(得分:1)
<强> HTML 强>
<input id="btnDetail" type="button" class="btn btn-success btn-sm" value="Details" />
<div id="Detail"></div>
<强> JS 强>
$('#btnDetail').on('click', function(){
$.ajax({
url: '@Url.Action("ShowTaskByID")',
data: { id: showTaskID },
}).done(function (data) {
$('#Detail').html(data);
$('#Detail').animate({
opacity: 1,
left: "+=50",
height: "toggle"
}, 3000, function () {
// Animation complete.
});
$('#Edit').hide();
$('#Delete').hide();
}).fail(function (jqXHR, textStatus) {
$('#Detail').html('<h3>Error :' + jqXHR.responseText + '</h3>');
});
});
<强> C#强>
public ActionResult Details(int id)
{
try
{
var task = db.Tasks.Find(id);
}
catch(HttpException e)
{
throw new HttpException(404, "Task not found.")
}
return View(task);
}
public PartialViewResult ShowTaskByID(int id)
{
try
{
var tasks = db.Tasks.Find(id).TaskName;
}
catch(HttpException e)
{
throw new HttpException(404, "Task nout found.")
}
return PartialView("_ShowTask", tasks);
}
如果您期待任务列表,请尝试以下方法:
public PartialViewResult ShowTaskByID()
{
try
{
var tasks = db.Tasks.ToList();
}
catch(HttpException e)
{
throw new HttpException(404, "Task nout found.")
}
return PartialView("_ShowTask", tasks);
}
或者,您可以将_ShowTask模型类型编辑为任务:
@model 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">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.TaskName)
</th>
<th>
@Html.DisplayNameFor(model => model.StartDate)
</th>
<th>
@Html.DisplayNameFor(model => model.FinishDate)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.DisplayFor(model => model.TaskName)
</td>
<td>
@Html.DisplayFor(model => model.StartDate)
</td>
<td>
@Html.DisplayFor(model => model.FinishDate)
</td>
<td>
@Html.ActionLink("Edit", "EditTask", new { id = Model.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 = Model.TaskId }, new { @class = "btn btn-danger btn-xs" })
</td>
</tr>
</tbody>
} @* closing of if *@
</table>
}
else
{
<div><strong>No Actors in Movie</strong></div>
}
</div>
答案 1 :(得分:0)
[HTTPGET]
将标记添加到public PartialViewResult ShowTaskByID(int id)
所以,结果如下:
[HttpGet]
public PartialViewResult ShowTaskByID(int id)