簡體   English   中英

使用onclick屬性將值(字符串)形式的剃刀模型傳遞給JS

[英]Passing a value(string) form razor model to JS with onclick attribut

這是我的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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM