How to pass empty string as a parameter to MVC ActionMethod from Javascript

I have an 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);
        }

my javascript method

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

when I get notificationName value then its hitting my ActionMethod but when I get notificationName as empty strings**(i.e; notificationName=”)** its not hitting my endpoint. Instead, its hitting another endpoint which looks like

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

Could someone help me with this issuw

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Change your action to this:

          [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();
}

and your javascript maybe need some changes too:

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

Method 2

I would suggest that you add another Method in Server

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x