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

时间:2013-12-09 17:42:51

标签: c# javascript asp.net-mvc twitter-bootstrap kendo-ui

我在我的视图页面(剃刀)上使用了kendolistview。在剑道模板里面我有这个代码。它只是打开引导弹出窗口。问题是"内容"数据有一些"和'值。所以链接不能被引用到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(视图模型)

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>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

每个不属于模板表达式的#符号 - #:#,##或#=# - 必须转义\#。

Link to docs