【发布时间】:2020-08-19 20:09:56
【问题描述】:
我有一个父视图和一个局部视图。
局部视图是 2 个伪按钮 - 拇指向上和拇指向下。一个分配了类的段落。
它没有响应点击任一拇指。也没有进入应该在“就绪”时执行的 2 个函数。
它在渲染时进入局部视图中的 jQuery。但并不完全,因为我没有看到我输入的所有控制台日志或警报。
我获得了“就绪”功能的警报和控制台日志,但没有其他功能。它不执行引用模型属性的 2 个 console.log 行,这些属性是布尔值并且每个值都为 false。
我收到此错误:jQuery.Deferred 异常:未定义 False ReferenceError:未定义 False。
但这似乎不是我的自定义代码。
局部视图有一个模型,其中我有我想使用 2 个布尔值以及整数的属性。
namespace GbngWebClient.Models
{
public class LikeOrDislikeVM
{
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; }
}
}
不确定是不是这个原因。我显示模型进来,false是小写进来的,这样符合JavaScript的要求。
包含部分视图的父视图(未显示所有代码):
@{
// Create the variable form the LikeOrDislikeVM model.
var likeOrDislikeVM = new GbngWebClient.Models.LikeOrDislikeVM();
// Set the properties on the variable - most from the parent model.
likeOrDislikeVM.BlogId = @Model.BlogPublishedByBlogId.BlogId;
likeOrDislikeVM.UserId = Convert.ToInt32(Session["UserId"]);
likeOrDislikeVM.LikeCount = @Model.BlogPublishedByBlogId.LikeCount;
likeOrDislikeVM.DisLikeCount = @Model.BlogPublishedByBlogId.DisLikeCount;
likeOrDislikeVM.LikeDisabled = @Model.BlogPublishedByBlogId.LikeDisabled;
likeOrDislikeVM.DisLikeDisabled = @Model.BlogPublishedByBlogId.DisLikeDisabled;
}
<div class="row">
<div class="col-md-1">
@Html.Partial("_BlogLikeAndDislike", likeOrDislikeVM)
</div>
</div>
局部视图:
@model GbngWebClient.Models.LikeOrDislikeVM
<style>
.fa {
cursor: pointer;
user-select: none;
}
.fa:hover {
color: blue;
}
.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 () {
alert('Here at document ready');
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');
$(".blogLike").attr('disabled', disabledSwitch);
//if (disabledSwitch == false )
//{
// $(".blogLike").color('green');
//}
}
function SetDisLike(disabledSwitch) {
alert('Here at SetDisLike');
$(".blogDisLike").attr('disabled', disabledSwitch);
//if (disabledSwitch == false) {
// $(".blogDisLike").color('green');
//}
}
});
</script>
在模型属性周围加上刻度。代码现在执行,但布尔值是 False 而不是 false。然而,模型屏幕截图显示它们是错误的。 JavaScript 需要它们为假。为什么会这样?
【问题讨论】:
-
根据第一个屏幕截图,您需要解决各种 JavaScript 错误。其中任何一项都可能阻止代码进一步执行。
-
David...是的,我看到了这些,但不知道如何解决它们,因为它们似乎是库错误。我删除了我的 2 .on click 函数,只留下了我的 2 组自定义函数以查看它是否是我的自定义代码。仍然不执行 console.log 行或输入任何一个简单的自定义函数。
-
2 个控制台行引用了模型属性,这些属性是布尔值,每个属性的值都为 false。我明白了:jQuery.Deferred 异常:未定义 False ReferenceError:未定义 False。
-
大卫……越来越近了。我必须在模型属性周围添加刻度。如console.log('@Model.LikeDisabled');和 SetLike('@Model.LikeDisabled');代码现在执行。但是,问题在于该值是 False 而不是 false。然而,模型本身具有 false 的属性。 JavaScript 需要它们为 false。我包括了上面的屏幕截图。
标签: jquery asp.net-mvc