【问题标题】:How to pass empty string as a parameter to MVC ActionMethod from Javascript如何将空字符串作为参数从 Javascript 传递给 MVC ActionMethod
【发布时间】:2021-01-12 17:24:20
【问题描述】:

我有一个 ActionMethod

        [HttpGet]
        [Route("ControllerName/IsUniqueNotificationName/{notificationName}")]
        public IActionResult IsUniqueNotificationName(string notificationName)
        {
            var name = string.IsNullOrEmpty(notificationName);
            var isUnique = 
              this.bannerNotificationService.IsUniqueNotificationName(notificationName);
            return this.Json(isUnique);
        }

我的 javascript 方法

        checkUniqueNotificationname = function (emailElement) {
        var notificationName = $(emailElement).val();
        let uri = "BannerNotification/IsUniqueNotificationName/" + notificationName;
        $.ajax({
            type: "Get",
            url: common.buildUrlWithBasePath(uri),
            success: function (data) {
                if (data == true) {
                    $("#BannerNotificationName").css({ "border-color": "black" });
                    $("#altFromMessageGroupValue").hide();
                }
                else {
                    $("#BannerNotificationName").css({ "border-color": "red" });
                    $("#altFromMessageGroupValue").show();
                }
            },
            error: function () {
                common.hideLoader();
            }
        });
    };

当我得到 notificationName 值时,它会击中我的 ActionMethod,但是当我将 notificationName 作为空字符串时**(即;notificationName='')** 它没有击中我的端点。相反,它击中了另一个端点,看起来像

        [Route("ControllerName/{banName}")]
        [HttpGet]
        public IActionResult Details(string banName)
        {
        }

有人可以帮我解决这个问题

【问题讨论】:

    标签: javascript c# asp.net asp.net-mvc model-view-controller


    【解决方案1】:

    将您的操作更改为:

              [HttpGet]  
            [Route("ControllerName/IsUniqueNotificationName/{notificationName?}")]
            public IActionResult IsUniqueNotificationName(string notificationName)
            {
                var empty = string.IsNullOrEmpty(notificationName);
    if (! empty){
    }
                var isUnique = 
                  this.bannerNotificationService.IsUniqueNotificationName(notificationName);
                return  Json(isUnique);
            }
    else return BadRequest();
    }
    

    您的 javascript 可能也需要一些更改:

    checkUniqueNotificationname = function (emailElement) {
            var notificationName = $(emailElement).val();
           if(notificationName =="")
    {
       $("#BannerNotificationName").css({ "border-color": "red" });
         $("#altFromMessageGroupValue").show();
    return false;
    }
    .....your code
      };
    

    【讨论】:

      【解决方案2】:

      我建议你在服务器中添加另一个方法

      [HttpGet]
          [Route("ControllerName/IsUniqueNotificationName")]
          public IActionResult IsUniqueNotificationName()
          {
              var name = "";
              var isUnique = 
                this.bannerNotificationService.IsUniqueNotificationName(name);
              return this.Json(isUnique);
          }
      

      【讨论】:

        猜你喜欢
        • 2022-01-09
        • 2017-04-25
        • 2020-01-01
        • 1970-01-01
        • 2014-03-03
        • 2016-09-08
        • 1970-01-01
        • 2018-07-11
        • 1970-01-01
        相关资源
        最近更新 更多