如果会话中的属性不为 null 或默认值,则运行/打印 JavaScript Razor ASP.NET MVC

If property at session not null or default value, run/print javascript Razor ASP.NET MVC

本文关键字:打印 运行 JavaScript Razor MVC NET ASP 默认值 会话 属性 null      更新时间:2023-09-26

当我从会话访问模型并且属性值在 Razor 上不为空时如何打印 javascript?

这是我的代码:

@{ ARM.Models.LanIDModel lanIdModel = (LanIDModel)HttpContext.Current.Session["sessLanIDModel"]; }}

@if(lanIdModel.LanID!=0 && lanIdModel.Name != string.Empty &&  !string.IsNullOrEmpty(lanIdModel.Email)){
           @Html.Raw("$('.input-switch-disabled').removeAttr('disabled');");
        }

当我在浏览器上运行时,我得到了这个:

 @if(lanIdModel.LanID!=0 && lanIdModel.Name != string.Empty && !string.IsNullOrEmpty(lanIdModel.Email)){
Line 98:            @Html.Raw("$('.input-switch-disabled').removeAttr('disabled');");
Line 99:         }

它能做到吗?

将 javascript 包装在脚本标签中。

@Html.Raw("<script>$('input-switch-disabled').removeAttr('disabled');</script>");