【问题标题】:I am not getting the alert javascript on my controller page我没有在我的控制器页面上收到警报 javascript
【发布时间】:2011-11-17 03:29:28
【问题描述】:

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

这是我做错了吗?

谁能帮帮我。

谢谢

【问题讨论】:

    标签: javascript jquery asp.net-mvc json asp.net-mvc-2


    【解决方案1】:

    我的东西,这行有一个错误:

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

    更正:

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

    【讨论】:

      【解决方案2】:

      如果您愿意,可以记下我的答案,但普遍认为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 的问题,但它应该得到代码的预期结果。

      【讨论】:

        【解决方案3】:

        您的问题可能源于您的客户端 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");
                }
            };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-19
          相关资源
          最近更新 更多