Javascript 如果条件在使用tempdata的脚本中不起作用
控制器-Javascript 如果条件在使用tempdata的脚本中不起作用,javascript,model-view-controller,Javascript,Model View Controller,控制器- public ActionResult ChechUserNP(Login dd) { if (dd.userid == "admin" && dd.pwd == "admin") { return RedirectToAction("Index", "DataGridView"); } else {
public ActionResult ChechUserNP(Login dd)
{
if (dd.userid == "admin" && dd.pwd == "admin")
{
return RedirectToAction("Index", "DataGridView");
}
else
{
TempData["Message1"] = 1;
return RedirectToAction("Index", "Login");
}
}
该控制器将tempdata
发送到视图(html)
调试器;
$(文档).ready(函数(){
message='@TempData[“Message1”]';
如果(消息!==null){
toastr.options={
“closeButton”:正确,
“调试”:错误,
“progressBar”:没错,
“防止重复”:正确,
“positionClass”:“右上角烤面包”,
“onclick”:空,
“showDuration”:“400”,
“隐藏”:“1000”,
“超时”:“7000”,
“extendedTimeOut”:“1000”,
“挥杆”:“挥杆”,
“隐藏”:“线性”,
“showMethod”:“fadeIn”,
“隐藏法”:“淡出”
}
toastr.error('用户名或密码错误')
}
});
如果用户ID或密码错误,它将返回TempData[“Message1”]=1代码>。在脚本上,它将检查它是否有值,以及是否执行toastr。但是,当我在开始时加载页面时,即使包含了if(message!==null)
,toastr也会工作
所以我只想在userid
或password
错误时执行toastr。
我的代码有什么问题,它是针对我的学校项目的看起来你是在通过模板系统将值插入Javascript字符串中。在Javascript中,无论发生什么情况,该值都将是一个字符串,至少是message=''代码>。哪个不是null
…!?有解决方案吗?检查两次Javascript中的实际值,并相应地调整测试…?或者以不同的方式传递值,使其实际为Javascriptnull
;如何做到这一点,我们不能肯定地告诉你,不知道模板的东西是什么。
<script>
debugger;
$(document).ready(function () {
message = '@TempData["Message1"]';
if (message !== null) {
toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
"preventDuplicates": true,
"positionClass": "toast-top-right",
"onclick": null,
"showDuration": "400",
"hideDuration": "1000",
"timeOut": "7000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr.error('Username or Password is wrong')
}
});
</script>