2015-11-25 50 views
1

我對ASP .NET來說很新,所以我所有的術語可能都不正確 - 請和我一起裸露。
我想要的是,我想檢查數據庫中是否有文件。要做到這一點,我有一個控制器中的方法,檢查這個並返回一個布爾值爲相應的情況。它看起來像這樣:從控制器獲取返回值到javascript

public bool fileInDb(int empId) 
    { 
     using (SLADbContext db = new SLADbContext()) 
     { 
      bool file = db.CompetenceUploads.Any(x => x.EmployeeId == empId); 
      if (file) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 

我只是檢查是否有任何文件分配給給定的員工。

現在我想從視圖中的javascript中調用此方法,並獲取返回值,以便我可以讓用戶知道是否有文件分配給選定的員工。它可能看起來像這樣:

$("#get-file").click(function() { 

     empId: $("#EmployeeSelect").val(); 
     var fileInDb = // Get the return value from the method 'fileInDb' 

     if(fileInDb) { 
      // Let the user download the file he/she requested 
      var url = "@Url.Action("GetUploadedFile", "Competence")"; 
      this.href = url + '?empId=' + encodeURIComponent($("#EmployeeSelect").val()); 
     } else { 
      alert("There is no file assigned to this employee."); 
     } 
    }); 

所以我現在的問題是,我怎麼從控制器的方法得到的返回值?

預先感謝您!

回答

1

我建議這裏一些變化:

更改controller方法有returnActionResultJsonResult,我更喜歡JsonResult就足夠了這裏retrun從controllerJson響應和用$.get操作此方法。您還需要將參數更改爲string,因爲將收到parameter作爲Json string。現在

public JsonResult fileInDb(string eId) //change signature to string and then convert to int 
{ 
    int empId=Convert.ToInt32(eId); 
    using (SLADbContext db = new SLADbContext()) 
    { 
     bool file = db.CompetenceUploads.Any(x => x.EmployeeId == empId); 
     if (file) 
     { 
      return Json(new { result = true },JsonRequestBehavior.AllowGet); 
     } 
     else 
     { 
      return Json(new { result = false},JsonRequestBehavior.AllowGet); 
     } 
    } 
} 

您的通話ajax-get將如下:

$("#get-file").click(function() { 
    var eId= $("#EmployeeSelect").val(); 
    $.get('/YourControllerName/fileInDb',{'eId':eId},function(response){ 
     //you just need to get the response so $.get is enough to manipulate 
     //this will be called once you get the response from controller, typically a callback 
     if(response.result) //same result variable we are returning from controller. 
     { 
      // Let the user download the file he/she requested 
      var url = "@Url.Action("GetUploadedFile", "Competence")"; 
      this.href = url + '?empId=' + encodeURIComponent($("#EmployeeSelect").val()); 
     } else { 
      alert("There is no file assigned to this employee."); 
     } 
    }) 
}); 
+0

感謝您的答覆!這似乎是一個很好的解決方案。然而,我對「響應」參數感到困惑 - 它是什麼,它是「真正的」參數,還是它被替換? :) –

+0

不。保持它原樣..它只是一個參考服務器響應.. :)你可以給你自己的名字.. :) –

0

您需要使用ASP fileInDb函數設置單頁腳本,然後使用瀏覽器中的AJAX與該頁進行通信。如果您不熟悉AJAX,我建議您使用jQuery implementation來開始。

0

在後臺創建調用一個方法,在JavaScript代碼中使用$就

 $.ajax({ 
      cache: false, 
      url: '@Url.Action("fileInDb")', 
      data: { 'empId': someVar }, 
      type: 'POST', 
      success: function (response) { 
        if (response.Data.FileExists === true) { 
        // do something 
        } else { 
        // it was false 
        } 
       }, 
      error: function (er) { 
       alert('Error!' + er); 
      } 
     }); 
0

您可以使用jQuery和Ajax實現這個返回JsonResult

public JsonResult fileInDb(int empId) 
    { 
     // your code - set fileExists to true/false 

     JsonResult returnObj = new JsonResult 
     { 
      Data = new 
      { 
       FileExists = fileExists ; 
      } 
     }; 

     return Json(returnObj); 
    } 

。使用客戶端代碼中的ajax調用來調用您的方法。下面是一個例子作爲參考:Calling controller method from view