Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

c# - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...