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

Ofl*_*cet 3 c# asp.net-mvc razor asp.net-mvc-3

我正在尝试从我的视图中调用一个方法来更改我的数据库中的布尔值,我唯一不知道的事情是因为我不熟悉MVC,每当我调用我的控制器方法时它会给我一个空白页面.我只是想调用方法,但保留在实际视图中.

以下是我视图中的代码部分.

<td><a href="@Url.Action("PutInBin", "Capture", new { captureId = @Model.Files.Captures.ElementAt(i).Capture_Id })", onclick="DeleteCapture(@(i + 1))">Delete</a></td>
Run Code Online (Sandbox Code Playgroud)

这是我控制器中的方法

public void PutInBin(int captureId)
    {
        QueryCaptureToBin queryCaptureToBin = new QueryCaptureToBin();
        queryCaptureToBin.Capture_Id = captureId;
        client.PlaceCaptureInBin(queryCaptureToBin, userParams);
    }
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 10

你可以使用AJAX:

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

并且不要忘记将jquery.unobtrusive-ajax.js脚本包含在您的页面中:

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

和你的控制器动作:

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

如果您想在删除完成时收到通知:

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

然后你会有你的onDeleteSuccessjavascript函数:

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');
};
Run Code Online (Sandbox Code Playgroud)