【问题标题】:MVC: How to: Clicking on a button without leaving current formMVC:如何:在不离开当前表单的情况下单击按钮
【发布时间】:2016-01-11 03:16:05
【问题描述】:

我有以下看法:

@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,当我执行一些逻辑以从模型中删除数据时,用户将停留在当前屏幕上。

这是界面。单击图标“glyphicon glyphicon-trash”应该在我的控制器上执行一些操作,而无需刷新或离开当前屏幕。这可能吗?

【问题讨论】:

  • 让它成为一个ajax调用。
  • 在不离开当前页面的情况下发出 HTTP 请求,这将是由 JavaScript 代码发出的 AJAX 请求。
  • 不幸的是我不熟悉javascript,如果你能指出我需要看的正确方向或教程。将不胜感激。

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


【解决方案1】:

是的,使用 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
          }
    });
});

现在在行动级别,像这样处理它:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2015-05-23
    相关资源
    最近更新 更多