这是我的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 Method对字符串进行编码:

@{
    var mystring = Html.Raw(HttpUtility.JavaScriptStringEncode(Model.NewLog.CallLogText));
}

10-02 14:49
查看更多