MVC:如何:单击按钮而不离开当前表单

时间:2015-10-13 11:29:06

标签: asp.net-mvc model-view-controller

我有以下观点:

@if (!string.IsNullOrWhiteSpace(ptiFileName))
{
    <span style="font-style:italic">
        <a href="@Url.Action("DownloadPtiDocument", "Project")" style="text-decoration:underline">@ptiFileName</a>
        <a class="glyphicon glyphicon-trash" href="@Url.Action("DeletePtiDocument", "Project")" type=""></a>
    </span>         
}

当用户点击文件名链接时, DownloadPtiDocument 的控制器正常工作:

   [Route("project/download/{id:int}")]
    public async Task<ActionResult> DownloadPtiDocument(int id)
    {

        var ptiFile = ProjectRepository.GetById(id.ToString()) as Project;

        if (ptiFile.PtiUploadDocuments == null)
            return HttpNotFound();

        var info = await S3Helpers.DownloadFile(ptiFile.PtiUploadDocuments.S3Bucket, ptiFile.PtiUploadDocuments.S3FileName);
        if (info != null)
            return new FileStreamResult(info.Stream, info.ContentType) { FileDownloadName = ptiFile.PtiUploadDocuments.FileName };

        return HttpNotFound();
    }

但是我不知道如何声明函数 DeletePtiDocument ,当我执行一些逻辑从我的模型中删除数据时,用户将留在当前屏幕上。

这是界面。点击图标&#34; glyphicon glyphicon-trash&#34;应该在我的控制器上执行一些操作而不刷新或离开当前屏幕。这有可能吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

是的,使用Ajax绝对可以这样做:

$('a.glyphicon-trash').click(function(){
    $.post('@Url.Action("DeletePtiDocument","Project")',{//Arguments you want to take there},function(data){
          if(data == "Success")
          {
              //Do whatever you want to do
          }
    });
});

现在在Action级别,按照以下方式处理:

[HttpPost]
public ActionResult DeletePtiDocument(arguments which you have passed)
{
    //perform Action
    return Json("Success");
}