在 MVC 中调用方法而不加载视图
我目前正在尝试从我的视图调用一个方法来更改数据库中的布尔值,由于我不太熟悉 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);
}
I am currently trying to call a method from my view to change a boolean in my database, the only thing i don't know since i am not really familiar with MVC is that whenever i call my controller method it gives me a blank page. I just want to call the method but stay in the actual view.
Here is the part of code in my view.
<td><a href="@Url.Action("PutInBin", "Capture", new { captureId = @Model.Files.Captures.ElementAt(i).Capture_Id })", onclick="DeleteCapture(@(i + 1))">Delete</a></td>
And here is the method in my controller
public void PutInBin(int captureId)
{
QueryCaptureToBin queryCaptureToBin = new QueryCaptureToBin();
queryCaptureToBin.Capture_Id = captureId;
client.PlaceCaptureInBin(queryCaptureToBin, userParams);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 AJAX:
并且不要忘记将
jquery.unobtrusive-ajax.js
脚本包含到您的页面中:以及您的控制器操作:
以及如果您想在删除完成时收到通知:
以及那么你将拥有
onDeleteSuccess
javascript 函数:You could use AJAX:
and don't forget to include the
jquery.unobtrusive-ajax.js
script to your page:and your controller action:
and if you wanted to get notification when the delete finishes:
and then you would have your
onDeleteSuccess
javascript function:来刷新页面。请使用下面的java脚本函数。与上面相同,但删除了“alert”并使用了 window.location.reload(true)。在 html 或 cshtml 视图中使用。向原主人致敬
To refresh the page. Please use below java script function. It's same as above but removed "alert" and used window.location.reload(true). Use in html or cshtml view. Hats off original owner