如何解决JavaScript与boolean false配合不好
在我的局部视图中,我有一个具有布尔属性的模型,该模型将传递给JavaScript函数。
namespace GbngWebClient.Models
{
public class LikeOrDislikeVM
{
// Auto-implemented properties.
public int BlogId { get; set; }
public int UserId { get; set; }
public int LikeCount { get; set; }
public int DisLikeCount { get; set; }
public bool LikeDisabled { get; set; }
public bool DisLikeDisabled { get; set; }
}
}
我将模型布尔属性传递给JavaScript函数。
@model GbngWebClient.Models.LikeOrDislikeVM
<style>
.fa {
cursor: pointer;
user-select: none;
}
.fa:hover {
color: blue;
}
/* I added. */
.my-size {
font-size: 20px;
}
</style>
@* Use the values that were passed via a model. *@
<div class="row">
<p><span class="blogLike my-size fa fa-thumbs-up"></span><span class="my-size"> : @Model.LikeCount</span> <span class="my-size"> | </span><span class="blogDisLike my-size fa fa-thumbs-down"></span><span class="my-size"> : @Model.DisLikeCount</span></p>
</div>
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
$(document).ready(function () {
console.log('Here at document ready');
console.log('@Model.LikeDisabled');
console.log('@Model.DisLikeDisabled');
SetLike('@Model.LikeDisabled');
SetDisLike('@Model.DisLikeDisabled');
$('.blogLike').on('click',function () {
alert('Here at like');
$.ajax({
type: 'POST',url: '@Url.Action("SetBlogLikeOrDisLike","BlogPublished")',data: { likeOrDislikeVM: @Model,likeOrDislikeIndicator: "L"},success: function (response) {
},error: function (xhr,ajaxOptions,thrownError) {
alert("Critical Error: something is wrong in the call to SetBlogLikeOrDisLike for a Like! Status: " + xhr.status + ". Error: " + thrownError.toString() + ". Response Text: " + xhr.responseText);
}
})
});
$('.blogDisLike').on('click',function () {
$.ajax({
type: 'POST',likeOrDislikeIndicator: "D"},thrownError) {
alert("Critical Error: something is wrong in the call to SetBlogLikeOrDisLike for a DisLike! Status: " + xhr.status + ". Error: " + thrownError.toString() + ". Response Text: " + xhr.responseText);
}
})
});
function SetLike(disabledSwitch) {
//alert('Here at SetLike');
console.log('Here at SetLike');
console.log('disabledSwitch ' + disabledSwitch);
$(".blogLike").attr('disabled',disabledSwitch);
if (disabledSwitch == false )
{
console.log('True');
$(".blogLike").color('green');
}
}
function SetDisLike(disabledSwitch) {
//alert('Here at SetDisLike');
console.log('Here at SetDisLike');
console.log('disabledSwitch ' + disabledSwitch);
$(".blogDisLike").attr('disabled',disabledSwitch);
if (disabledSwitch == false)
{
console.log('True');
$(".blogDisLike").color('green');
}
}
});
</script>
代码已执行,但布尔值是False而不是false。但是,上面的模型屏幕截图显示它们为假。
JavaScript要求它们为false。为什么会发生这种情况以及如何解决?
解决方法
看起来.NET正在将它们作为字符串返回:https://docs.microsoft.com/en-us/dotnet/api/system.boolean?view=netcore-3.1
在javascript中,'False' !== false
@ jaromanda-x的评论将是处理它的正确方法。
,根据第二个屏幕截图,@Model.LikeDisabled
传递的值为False,它是一个字符串,而不是false
,它是布尔属性。我建议您先验证属性,然后再将它们存储在变量中,然后使用变量设置SetLike('@Model.LikeDisabled')
。
var like;
if('@Model.LikeDisabled' === 'False') {
like = false;
} else { like = true; }
SetLike(like)
与不喜欢的属性相同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。