2015-10-13 23 views
0

我有以下看法:MVC:如何:一個按鈕點擊無需離開當前的形式

@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」應該在我的控制器上執行一些操作,而無需刷新或離開當前屏幕。這可能嗎?

enter image description here

+1

使它成爲阿賈克斯呼叫。 – dansasu11

+0

要在不離開當前頁面的情況下發出HTTP請求,它將是由JavaScript代碼構成的AJAX請求。 – David

+0

不幸的是,我不熟悉JavaScript,如果你可以指點我正確的方向或教程,我需要看看。這將非常感激。 – JADE

回答

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 
      } 
    }); 
}); 

現在就行動水平,處理這樣的:

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