使用onclick属性将值(字符串)形式的剃刀模型传递给JS

时间:2018-12-05 10:54:19

标签: javascript c# razor model-view-controller

这是我的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事件

1 个答案:

答案 0 :(得分:1)

在传递给JS之前,使用HttpUtility.JavaScriptStringEncode Method对字符串进行编码:

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