Welcome to OGeek Q&A 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...