使用javascript检查viewmodel上的真/假条件
我使用本地存储变量保存用户当前进度的位置。我遇到了一个问题,即如果用户所在的最后一个部分已被删除,则必须将目标调用设置为错误。这是我的代码:使用javascript检查viewmodel上的真/假条件,javascript,jquery,local-storage,viewmodel,Javascript,Jquery,Local Storage,Viewmodel,我使用本地存储变量保存用户当前进度的位置。我遇到了一个问题,即如果用户所在的最后一个部分已被删除,则必须将目标调用设置为错误。这是我的代码: if (localStorage["Course" + '@Model.Course.CourseID'] != null && localStorage["Course" + '@Model.Course.CourseID'] != "") { var id = localStorage["Course" + '
if (localStorage["Course" + '@Model.Course.CourseID'] != null && localStorage["Course" + '@Model.Course.CourseID'] != "") {
var id = localStorage["Course" + '@Model.Course.CourseID'];
}
else {
var id = '@Model.CourseSections.First().CourseSectionID';
}
我需要使用javascript检查数据库中是否仍然存在localStorage课程部分,因此我创建了以下ViewModel方法:
public bool CourseSectionLaunchStillExistCheck(int courseSectionID)
{
this.TargetCourseSection = courseSectionRepository.Get(cs => cs.CourseSectionID == courseSectionID).FirstOrDefault();
if (this.TargetCourseSection != null)
{
return true;
}
else
{
return false;
}
}
但当我尝试使用以下javascript时:
if (localStorage["Course" + '@Model.Course.CourseID'] != null && localStorage["Course" + '@Model.Course.CourseID'] != "") {
var id = localStorage["Course" + '@Model.Course.CourseID'];
if ('@Model.CourseSectionLaunchStillExistCheck(id)' != true) {
var id = '@Model.CourseSections.First().CourseSectionID';
}
}
else {
var id = '@Model.CourseSections.First().CourseSectionID';
}
它无法识别表示当前上下文中不存在的id参数。在设置变量之前,如何使用javascript确保课程部分存在
我是否可以使用以下帖子:
var postData = { 'courseSectionID': id };
$.post('/Course/CourseSectionLaunchStillExistCheck/', postData, function (data) {
});
然后,我如何检查此帖子数据的结果是真是假?你不能这样做。Razor/C#在服务器上执行,javascript稍后在客户端执行。我可以用javascript发布它吗?然后检查返回?是的,您可以使用ajax调用服务器。但是,由于您需要服务器上的这些信息,那么从客户端和服务器上都可以访问的cookie是更好的解决方案吗?我正在与其他人的代码对抗,只需要在过渡期间对此进行修复,但继续使用cookie是更好的解决方案,只是,若我现在改变它,它将需要工作,通过什么我可以检查后的结果是真实的?