我计划使用一些 Model
属性来管理 HTML/Razor 中的 JavaScript 代码。
"columnDefs": [
{
"render": function (data, type, row) {
@if (Model.ConfigViewModel.AllowDeleteAnyUsers)
{
return '<center><img class="cursor-hand" onclick="javascript:ConfirmRemoveUser(this)" data-userId="' + row.Id + '" data-userName="' + row.UserName + '" src="images/delete.png" width="16" height="16" border="0"></center>'
}
else
{
return '<center><img class="cursor-hand" onclick="javascript:ConfirmRemoveUser(this)" data-userId="' + row.Id + '" data-userName="' + row.UserName + '" src="images/delete-disabled.png" width="16" height="16" border="0"></center>'
}
},
"targets": 3
}
但是像 row.Id
这样的 JS 代码元素会给出这样的错误。
Compiler Error Message: CS1012: Too many characters in character literal
知道如何结合 Razor 和 JS 吗?
最佳答案
尝试这样写:
@if (Model.ConfigViewModel.AllowDeleteAnyUsers)
{
<text>
return '<center><img class="cursor-hand" onclick="javascript:ConfirmRemoveUser(this)" data-userId="' + row.Id + '" data-userName="' + row.UserName + '" src="images/delete.png" width="16" height="16" border="0"></center>';
</text>
}
关于javascript - 结合 JavaScript 和 ASP.NET MVC HTML 代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006272/