我的控制器页面上没有收到警报javascript

我的控制器页面上没有收到警报javascript,javascript,jquery,asp.net-mvc,json,asp.net-mvc-2,Javascript,Jquery,Asp.net Mvc,Json,Asp.net Mvc 2,我的ActionResult中有此代码 public ActionResult Copy( int bvVariableid ) { var iReturn = _bvRepository.CopyBenefitVariable( bvVariableid, CurrentHealthPlanId, CurrentControlPlanId, _bvRepository.GetSecInfo( ).UserId, IsNascoUser()); i

我的ActionResult中有此代码

public ActionResult Copy( int bvVariableid ) {
            var iReturn = _bvRepository.CopyBenefitVariable( bvVariableid, CurrentHealthPlanId, CurrentControlPlanId, _bvRepository.GetSecInfo( ).UserId, IsNascoUser());
            if (iReturn == -999)
                return new JavaScriptResult() { Script = "alert(Unique variable name could not be created');" };
            if( iReturn != -1 )
                return Json( new { RedirectUrl = string.Format( "/BvIndex/Index/{0}?bvIndex-mode=select", iReturn ) } );
            return RedirectToRoute( "Error" );
        }
这就是我认为的代码

CopyBenefitVariable = function (bvId, bvName) {
            if (confirm('Are you sure you want to copy from the Benefit Variable ' + bvName + ' ?')) {
                $.post(
              "/BvIndex/Copy/",
              { bvVariableid: bvId },
              function (data) {
                  window.location = data.RedirectUrl;
              }, "json");
            }
        };
当IReturn为-999时,我的页面上不会出现JavaScriptResult警报框

这是我做错了什么吗

有人能帮我吗


谢谢

我的东西,这行有一个bug:

return new JavaScriptResult() { Script = "alert(Unique variable name could not be created');" };
更正:

  return new JavaScriptResult() { Script = "alert('Unique variable name could not be created');" };

如果你愿意,你可以记下我的答案,但是人们普遍认为,
JavaScriptResult
对于ASP.NET MVC团队来说是一个糟糕的举动。也就是说,您的示例已经为您的一个条件返回了
Json
操作结果。您可以对这两个项目执行相同的操作。如果将JSON对象更改为:

 return Json( new { success = bool,  RedirectUrl = value } );
然后,您可以将客户端功能更改为:

function (data) {
  if(data.success === true) {
    window.location = data.RedirectUrl;
  } else {
    alert('Unique variable name could not be created');
  }
}

我知道它不能直接解决
JavaScriptResult
的问题,但它应该得到代码的预期结果。

您的问题可能源于客户端JavaScript。ajax中的.post()方法实际上是以下各项的快捷方式:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
因此,客户端代码告诉jQuery将结果解释为json对象(即使您发回了脚本)

我会将您的代码更改为如下所示:

public ActionResult Copy( int bvVariableid ) {
    var iReturn = _bvRepository.CopyBenefitVariable( bvVariableid, CurrentHealthPlanId, CurrentControlPlanId, _bvRepository.GetSecInfo( ).UserId, IsNascoUser());

    if (iReturn == -999)
        return new Json(new { type = "msg", data = "Unique variable name could not be created" });
    if( iReturn != -1 )
        return Json( new { type = "url", data = string.Format( "/BvIndex/Index/{0}?bvIndex-mode=select", iReturn ) } );
        return RedirectToRoute( "Error" );
}
CopyBenefitVariable = function (bvId, bvName) {
    if (confirm('Are you sure you want to copy from the Benefit Variable ' + bvName + ' ?')) {
        $.post(
          "/BvIndex/Copy/",
          { bvVariableid: bvId },
          function (data) {
              if (data.type == "url") {
                  window.location = data.RedirectUrl;
              } else if (data.type == "msg") {
                  alert(data.data);
              }
          }, "json");
        }
    };
您的视图代码应该如下所示:

public ActionResult Copy( int bvVariableid ) {
    var iReturn = _bvRepository.CopyBenefitVariable( bvVariableid, CurrentHealthPlanId, CurrentControlPlanId, _bvRepository.GetSecInfo( ).UserId, IsNascoUser());

    if (iReturn == -999)
        return new Json(new { type = "msg", data = "Unique variable name could not be created" });
    if( iReturn != -1 )
        return Json( new { type = "url", data = string.Format( "/BvIndex/Index/{0}?bvIndex-mode=select", iReturn ) } );
        return RedirectToRoute( "Error" );
}
CopyBenefitVariable = function (bvId, bvName) {
    if (confirm('Are you sure you want to copy from the Benefit Variable ' + bvName + ' ?')) {
        $.post(
          "/BvIndex/Copy/",
          { bvVariableid: bvId },
          function (data) {
              if (data.type == "url") {
                  window.location = data.RedirectUrl;
              } else if (data.type == "msg") {
                  alert(data.data);
              }
          }, "json");
        }
    };