我正在尝试从我的视图中调用一个方法来更改我的数据库中的布尔值,我唯一不知道的事情是因为我对MVC不是很熟悉,每当我调用我的控制器方法它给我一个空白页。我只想调用方法,但保留在实际视图中。
以下是我视图中的部分代码。
<td><a href="@Url.Action("PutInBin", "Capture", new { captureId = @Model.Files.Captures.ElementAt(i).Capture_Id })", onclick="DeleteCapture(@(i + 1))">Delete</a></td>
这是我控制器中的方法
public void PutInBin(int captureId)
{
QueryCaptureToBin queryCaptureToBin = new QueryCaptureToBin();
queryCaptureToBin.Capture_Id = captureId;
client.PlaceCaptureInBin(queryCaptureToBin, userParams);
}
答案 0 :(得分:10)
您可以使用AJAX:
<td>
@Ajax.ActionLink(
"Delete",
"PutInBin",
"Capture",
new {
captureId = Model.Files.Captures.ElementAt(i).Capture_Id
},
new AjaxOptions {
HttpMethod = "POST",
}
)
</td>
并且不要忘记将jquery.unobtrusive-ajax.js
脚本包含在您的网页中:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
和您的控制器操作:
[HttpPost]
public ActionResult PutInBin(int captureId)
{
QueryCaptureToBin queryCaptureToBin = new QueryCaptureToBin();
queryCaptureToBin.Capture_Id = captureId;
client.PlaceCaptureInBin(queryCaptureToBin, userParams);
return new EmptyResult();
}
如果您想在删除完成时收到通知:
<td>
@Ajax.ActionLink(
"Delete",
"PutInBin",
"Capture",
new {
captureId = Model.Files.Captures.ElementAt(i).Capture_Id
},
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = "onDeleteSuccess"
}
)
</td>
然后你会拥有onDeleteSuccess
javascript函数:
var onDeleteSuccess = function(result) {
// normally the result variable will contain the response
// from the server but in this case since we returned an EmptyResult
// don't expect to find anything useful in it.
alert('The capture was successfully deleted');
};
答案 1 :(得分:0)
刷新页面。请使用下面的java脚本函数。它与上面相同但删除了“alert”并使用了window.location.reload(true)。在html或cshtml视图中使用。帽子原主人
<script>
var onDeleteSuccess = function(result) {
// normally the result variable will contain the response
// from the server but in this case since we returned an EmptyResult
// don't expect to find anything useful in it.
window.location.reload(true);
// alert('The capture was successfully deleted');
};
</script>