在 MVC 中调用方法而不加载视图

发布于 2025-01-07 13:04:34 字数 618 浏览 1 评论 0原文

我目前正在尝试从我的视图调用一个方法来更改数据库中的布尔值,由于我不太熟悉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2025-01-14 13:04:34

您可以使用 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');
};

You could use AJAX:

<td>
    @Ajax.ActionLink(
        "Delete",
        "PutInBin", 
        "Capture", 
        new { 
            captureId = Model.Files.Captures.ElementAt(i).Capture_Id 
        },
        new AjaxOptions {
            HttpMethod = "POST",
        }  
    )       
</td>

and don't forget to include the jquery.unobtrusive-ajax.js script to your page:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

and your controller action:

[HttpPost]
public ActionResult PutInBin(int captureId)
{
    QueryCaptureToBin queryCaptureToBin = new QueryCaptureToBin();
    queryCaptureToBin.Capture_Id = captureId;
    client.PlaceCaptureInBin(queryCaptureToBin, userParams);
    return new EmptyResult();
}

and if you wanted to get notification when the delete finishes:

<td>
    @Ajax.ActionLink(
        "Delete",
        "PutInBin", 
        "Capture", 
        new { 
            captureId = Model.Files.Captures.ElementAt(i).Capture_Id 
        },
        new AjaxOptions {
            HttpMethod = "POST",
            OnSuccess = "onDeleteSuccess"
        }
    )       
</td>

and then you would have your onDeleteSuccess javascript function:

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');
};
遥远的她 2025-01-14 13:04:34

来刷新页面。请使用下面的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>

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


<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文