从Controller发送一个值给Jquery

Send a value from Controller to Jquery

本文关键字:一个 Jquery Controller      更新时间:2023-09-26

我有一个MVC应用程序。

控制器:

public ActionResult DescargaCsv()
{  
    // do stuff
    if (status != 0){
        return value to javascript and display an alert there
    }
    else{
        //do other stuff, this is OK
    }
}
javascript:

function fnDownloadExcel() {
$.ajax({
    url: fnDownloadExcel?idArchivo=" + $("#idArchivo").val(),
    type: "POST",        
    success: function (data) {
    $("#idMessage").val(data[0]);
    if (data[0] == "R") {
        alert("Status: " + $("#idMensaje").val());
        }
    else {
        //do other stuff
         }                        
    }
}); 

我如何发送到javascript的"状态"的值,我得到在控制器?

c#

public ActionResult DescargaCsv() {  
    // do stuff
    if (status != 0){
        return new HttpStatusCodeResult(500, "This is a bad status message");
    } else{
        //do other stuff, this is OK
    }
}
jQuery

function fnDownloadExcel() {
    $.ajax({
       url: fnDownloadExcel?idArchivo=" + $("#idArchivo").val(),
       type: "POST",        
       success: function (data) {
           $("#idMessage").val(data[0]);
           if (data[0] == "R") {
             alert("Status: " + $("#idMensaje").val());
          } else {
            //do other stuff
          }                        
       },
       statusCode: {
           500: function(data) {
                  alert(data);
           }
       }
    }); 
}

控制器:

public JsonResult DescargaCsv() {  
    // do stuff
    if (status != 0)
    {
        return Json(status);//return values you want 
    } 
    else
    {
        //do other stuff, this is OK
    }
}

脚本:

function fnDownloadExcel() {
    $.ajax({
       url: fnDownloadExcel?idArchivo=" + $("#idArchivo").val(),
       type: "POST",        
       success: function (data) {
           alert(data);    
       },
       statusCode: {
           500: function(data) {
              alert(data);
           }
       }
    }); 
}