获取Controller到javascript的返回值
Get return value from Controller to 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.");
}
});
所以我现在的问题是,如何从控制器中的方法获得返回值?
我建议在这里进行一些更改:
将controller
方法更改为return
类型为ActionResult
或JsonResult
,我更喜欢JsonResult
就足够了,然后从controller
重新运行Json
响应,并使用$.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.");
}
})
});
您需要使用ASP fileInDb
函数设置一个单页脚本,然后从浏览器使用AJAX与该页面通信。如果您不熟悉AJAX,我建议您使用jQuery实现来入门。
您可以使用jquery和ajax来实现这一点。使用客户端代码中的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);
}
在javascript代码中使用$.ajax
$.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);
}
});
相关文章:
- Javascript返回值只在循环中返回一次
- Javascript中带有返回值的嵌套函数
- JavaScript生成器中收益率返回值的解析
- HTML如何根据javascript函数的返回值限制文本输入
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 如何在Android中将Javascript返回值转换为String
- PHP 函数不会向 JavaScript 变量返回值
- 单击按钮时,使用Javascript调用并返回值
- 使用Javascript数组或JSON来存储AJAX请求的返回值,以备将来使用
- Javascript只返回键,不返回值
- 从php返回值到javascript
- Scala提升了如何运行javascript函数并将返回值存储在变量中
- Javascript 意外返回值,带有特定的缩进
- 如何在 Python 中获取此 JavaScript 函数的返回值
- 并非所有代码路径都返回值 (JavaScript)
- 难以分割数组并从其中返回值;Javascript
- 从嵌套函数返回值(Javascript)
- 为什么我得到逗号在我的返回值?JavaScript名人姓名揭示器
- 为什么我没有得到返回值JavaScript
- google maps -从嵌套函数返回值- JavaScript