我對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.");
}
});
所以我現在的問題是,我怎麼從控制器的方法得到的返回值?
預先感謝您!
感謝您的答覆!這似乎是一個很好的解決方案。然而,我對「響應」參數感到困惑 - 它是什麼,它是「真正的」參數,還是它被替換? :) –
不。保持它原樣..它只是一個參考服務器響應.. :)你可以給你自己的名字.. :) –