【发布时间】:2021-07-27 06:56:37
【问题描述】:
我正在使用带有 Razor 的 ASP.NET Core。
我有一个页面Education.cshtml 和Education.cshtml.cs。
在 Education.cshtml.cs OnGet() 处理程序中,我正在为变量赋值。在.cshtml 文件中,我尝试在window.onload 期间使用@Model.variable 访问变量。在window.onLoad JavaScript 中使用它,因为我必须在页面加载后以这个变量作为参数调用一个 JavaScript 函数。
但我收到一个参考错误:
未捕获的 ReferenceError:未定义“变量值” 在 window.onload
如何解决这个问题下面是我的代码。
在 Education.cshtml.cs 中:
[BindProperty]
public string degree { get; set; }
public async Task<IActionResult> OnGet()
{
degree = "XYZ";
}
在Education.cshtml:
@if (Model.EducationDetails?.successful == true)
{
<div class="alert alert-success" role="alert">
<h4>Deatils</h4>
<hr>
<strong>@Model.Education.StudentNumber</strong>
</div>
<script type="text/javascript">
window.onload = function () {
console.log('Model degree value -' + @Model.degree );
// callJavascriptFunction(@Model.degree); // Need to pass this @Model.degree to JS function
};
</script>
}
【问题讨论】:
标签: javascript c# asp.net-core razor