Json 来自 MVC 控制器的变量总是返回 "undefined"
Json variable from MVC Controller always returning "undefined"
我正在开发一个点网核心 MVC 应用程序。我的控制器具有以下功能:
[HttpPost]
public JsonResult Delete(string chkId)
{
if (chkId == "" || chkId == null)
StatusCode(StatusCodes.Status500InternalServerError);
List<string> stringList = new List<string>();
try
{
stringList = chkId.ToString().Split(',').ToList();
for (var i = 0; i < stringList.Count(); i++)
{
StringDA.Delete(Convert.ToInt64(stringList[i]), GetString());
}
string result = "Success";
return Json(result, new Newtonsoft.Json.JsonSerializerSettings());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
现在我的视图正在尝试使用以下 jquery.
访问函数
$('#btnDelete').on('click', function () {
// Code.........
var msg = "Are you sure you want to delete?";
var check = confirm(msg);
//alert(chkVal);
if (check == true) {
var join_selected_values = chkVal.join(",");
$.ajax({
url: "/General/Delete",
type: "POST",
cache: false,
dataType: 'json',
data: 'chkId=' + join_selected_values,
success: function (response) {
//alert(response.result);
if (response.result == "Success") {
alert("Record(s) deleted");
..........
}
else {
alert("Error in Appication");
}
}
});
}
}
});
但是当我测试我的应用程序时 response.resrult 总是抛出“未定义”,即使控制器功能正常。我不明白为什么 response.resrult 在我明确声明为“成功”时未定义?
你应该定义结果变量
return new JsonResult( new { result="Sucess" } );
我正在开发一个点网核心 MVC 应用程序。我的控制器具有以下功能:
[HttpPost]
public JsonResult Delete(string chkId)
{
if (chkId == "" || chkId == null)
StatusCode(StatusCodes.Status500InternalServerError);
List<string> stringList = new List<string>();
try
{
stringList = chkId.ToString().Split(',').ToList();
for (var i = 0; i < stringList.Count(); i++)
{
StringDA.Delete(Convert.ToInt64(stringList[i]), GetString());
}
string result = "Success";
return Json(result, new Newtonsoft.Json.JsonSerializerSettings());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
现在我的视图正在尝试使用以下 jquery.
访问函数$('#btnDelete').on('click', function () {
// Code.........
var msg = "Are you sure you want to delete?";
var check = confirm(msg);
//alert(chkVal);
if (check == true) {
var join_selected_values = chkVal.join(",");
$.ajax({
url: "/General/Delete",
type: "POST",
cache: false,
dataType: 'json',
data: 'chkId=' + join_selected_values,
success: function (response) {
//alert(response.result);
if (response.result == "Success") {
alert("Record(s) deleted");
..........
}
else {
alert("Error in Appication");
}
}
});
}
}
});
但是当我测试我的应用程序时 response.resrult 总是抛出“未定义”,即使控制器功能正常。我不明白为什么 response.resrult 在我明确声明为“成功”时未定义?
你应该定义结果变量
return new JsonResult( new { result="Sucess" } );