如何解决如何将空字符串作为参数从 Javascript 传递给 MVC 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)
{
}
谁能帮我解决这个问题
解决方法
我建议您在服务器中添加另一个方法
[HttpGet]
[Route("ControllerName/IsUniqueNotificationName")]
public IActionResult IsUniqueNotificationName()
{
var name = "";
var isUnique =
this.bannerNotificationService.IsUniqueNotificationName(name);
return this.Json(isUnique);
}
,
将您的操作更改为:
[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
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。