javascript - 使用 onclick 属性将值(字符串)形式的剃须刀模型传递给 JS
问题描述
这是我的html
@model MarketingPhoneBook.Models.Responses.ContactLogResponse
@{
Layout = "";
}
<form method="post" id="idForm" action="/Home/CreateLog">
<div class="form-group">
@{
var mystring = Html.Raw(Model.NewLog.CallLogText);
}
@Html.LabelFor(model => model.NewLog.CallLogText, htmlAttributes: new { @class = "control-label " })
<div onclick="addDate('@mystring')">
@Html.TextAreaFor(model => model.NewLog.CallLogText, new { @class = "", @style = "width: 100%; max-width: inherit;", @rows = "15", @onblur = "save()", @type = "submit" })
@Html.ValidationMessageFor(model => model.NewLog.CallLogText, "", new { @class = "text-danger" })
</div>
</div>
@Html.HiddenFor(model => model.NewLog.ContactId)
</form>
这是我的js函数
function addDate(text) {
return text = new Date() + " " + text;
}
Model.NewLog.CallLogText 是字符串类型。Onclick 事件不会被触发
解决方案
在传递给 JS 之前使用HttpUtility.JavaScriptStringEncode 方法对字符串进行编码:
@{
var mystring = Html.Raw(HttpUtility.JavaScriptStringEncode(Model.NewLog.CallLogText));
}
推荐阅读
- node.js - Heroku 部署后运行 URL 显示应用程序错误
- java - 如何跟踪 Firestore 读/写操作?
- git - VisualStudio 在 Rebase 时会创建非常不必要的合并冲突?
- c# - Azure Blob 存储的文件大小限制?
- kubernetes - 我可以在没有 virtualBox 的情况下在 ubuntu 上安装 minikube 吗?
- url - HAProxy http-request 将特定路径重定向到另一个路径
- android - Retrofit2:保护baseUrl
- python - 从熊猫系列中删除最小值
- python - Python 3 Tkinter:弹出窗口不显示
- android - 滑动和图像分辨率