I am working on a dot net core MVC application. My controller has the following function:
[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);
}
}
Now my View is trying to access the function using following 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");
}
}
});
}
}
});
But when I am testing my application response.resrult always throws "undefined" even controller function is okey. I can not understand why response.resrult is undefined when I am explicitly declaring it as "Success"?
CodePudding user response:
you should define result variable
return new JsonResult( new { result="Sucess" } );