我正在尝试创建一个表格标题,在单击时提醒其文本。我无法将 Razor 变量传递给 JavaScript 函数参数。什么是好的解决方法?
这是我的代码:
@foreach (var web in Model.Webinars)
{
var title = web.Filename;
if (!String.IsNullOrEmpty(title))
{
// If filename is too long, shorten it
if (title.Length > 10)
{
title = title.Substring(0, 10) + "...";
}
// Create a header that alerts when clicked
// Errors if I pass @title in alert()
<th onclick="alert(@title)">@title</th>
}
}
我收到如下所示的错误:
JavaScript critical error at line 71, column 34 in
http://localhost:64023/Webinar/Reports\n\nSCRIPT1010: Expected identifier
最佳答案
您需要将@title
括在引号中
<th onclick="alert('@title')">@title</th>
关于c# - 如何让 JavaScript 提醒 Razor 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733789/