簡體   English   中英

我沒有在控制器頁面上收到警報javascript

[英]I am not getting the alert javascript on my controller page

我的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警報框。

這是我在做錯什么嗎?

任何人都可以幫助我。

謝謝

我要說的是,這一行有一個錯誤:

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 Action Result。 您可以對兩個項目執行相同的操作。 如果您更改了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對象(即使您發送回腳本)。

$.post(
  "/BvIndex/Copy/",  // url
  { bvVariableid: bvId }, // data
  function (data) {
     window.location = data.RedirectUrl;  // success
  },
  "json" // dataType
);

我將更改您的代碼,如下所示:

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");
        }
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM