In my partial view, I have a model that has boolean properties that I will pass to JavaScript functions.
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; }
}
}
I pass the model boolean properties to JavaScript functions as such.
@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',
url: '@Url.Action("SetBlogLikeOrDisLike", "BlogPublished")',
data: { likeOrDislikeVM: @Model, likeOrDislikeIndicator: "D"},
success: function (response) {
},
error: function (xhr, ajaxOptions, 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>
The code executes but the boolean values are False instead of false. Yet the model screen shot above shows them as false.
JavaScript needs them to be false. Why is this happening and how to fix?