gpt4 book ai didi

c# - Javascript - 如何在 Kendo 模板上转义双引号和单引号

转载 作者:行者123 更新时间:2023-11-30 17:44:02 25 4
gpt4 key购买 nike

我在我的 View 页面 (razor) 上使用 kendolistview。在剑道模板中,我有这段代码。它只是打开 Bootstrap 弹出窗口。问题是“内容”数据有一些 "和 ' 值。因此链接无法引用到 js 函数,弹出窗口也不会打开。

我尝试替换数据层中的引号:

          Content = rss.CONTENT.Replace(@"\""", "\"").Replace("'", @"\'")
Summary = rss.SUMMARY.Replace(@"\""", "\"").Replace("'", @"\'")

查看

    @model AIS.UI.WebService.Proxy.DSrvAllService.NewsItem
@using Kendo.Mvc.UI

@(Html.Kendo().ListView<AIS.UI.WebService.Proxy.DSrvAllService.NewsItem>()
.Name("listView")
.TagName("div")
.ClientTemplateId("template")
.DataSource(dataSource => dataSource
.Model(model =>model.Id("ID"))
.ServerOperation(true)

.PageSize(2)
.Events(events => events.Error("onError"))
.Read(read => read.Action("GetNewsList", "News"))

)
.Pageable()


)



<script type="text/x-kendo-template" id="template">
....
<a
href="javascript:openNewsPopup('#:Title1#','#:Summary#','#:Content#','#:ImageURL#','#:AddTime#
','#:AddDate#','#:AddYear#','#:SubmitedBy#')" class="btn pull-right"><span style="font-
weight:normal" rel="tooltip" title="Read more about the announcement">Read More</span></a>

<script>

Js( View 模型)

var OpenNewsPopupViewModel = function() {
var self = this;

self.Title = ko.observable("");
self.Summary = ko.observable("");
self.Content = ko.observable("");
self.ImageURL = ko.observable("");
self.AddTime = ko.observable("");
self.AddDate = ko.observable("");
self.AddYear = ko.observable("");
self.SubmitedBy = ko.observable("");
self.ImageURLFull = ko.observable("");
};

var openNewsPopupViewModel = null;

function openNewsPopup(pTitle, pSummary, pContent, pImageURL, pAddTime, pAddDate, pAddYear, pSubmitedBy) {


var imageRootPath = '@Url.Content("~/Images/Announcements/NewsTypes/NewsType")';

var isNewViewModel = (openNewsPopupViewModel == null);

var newsPopup = $("#newsPopup");

if (isNewViewModel)
{
openNewsPopupViewModel = new OpenNewsPopupViewModel();

var newsPopupTag = newsPopup.get()[0];
ko.applyBindings(openNewsPopupViewModel, newsPopupTag);
}

// pContent = str.replace(/\"/g, "\\\"");
openNewsPopupViewModel.Title(pTitle);
openNewsPopupViewModel.Summary(pSummary);
openNewsPopupViewModel.Content(pContent);
openNewsPopupViewModel.ImageURL(pImageURL);
openNewsPopupViewModel.AddTime(pAddTime);
openNewsPopupViewModel.AddDate(pAddDate);
openNewsPopupViewModel.AddYear(pAddYear);
openNewsPopupViewModel.SubmitedBy(pSubmitedBy);
openNewsPopupViewModel.ImageURLFull("");
openNewsPopupViewModel.ImageURLFull(imageRootPath + '.' + openNewsPopupViewModel.ImageURL() + '.jpg');


newsPopup.modal('show');
}

示例数据(应替换为双引号)

  <a href="javascript:openNewsPopup(' Version 1  is Released','Version 1.0 
is now available on web. You can download the new version of the
client program from the website" http:="" 255.255.255.0="" test"="" or=""
installed="" program="" can="" automatically="" download="" it="" for=""
you.','news="" context="" will="" be="" placed="" here.="" news="" .=""
','update','12:04',="" '01.01','2013','admin')"="" class="btn pull-right"><span
style="font-weight:normal" rel="tooltip" title="Read more about the
announcement">Read More</span></a>

最佳答案

您需要在将字符串发送到客户端之前对其进行编码。如果在 Razor View 中设置字符串,您只需调用 @Html.Encode(string)。要对 Razor View 之外的内容进行编码(例如,在您的数据访问代码中),只需调用 System.Web.HttpUtility.HtmlEncode(string)直接。

HttpUtility.HtmlEncode("A simple 'encoded' \"string.\"");
// A simple &#39;encoded&#39; &quot;string.&quot;

关于c# - Javascript - 如何在 Kendo 模板上转义双引号和单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com