我需要在表数据中显示包含脚本和其他HTML标记的内容。但是当使用脚本标签时,控制台错误为
无法设置未定义的属性'_DT_CellIndex'
当我尝试将整个描述转换为字符串时。链接不起作用,它已转换为显示的字符串。
<
和>
替换为<
和>
型号:
using System;
using System.ComponentModel.DataAnnotations;
namespace HelloWorldMvcApp
{
public class SampleViewModel
{
}
}
查看:
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Bootstrap 101 Template</title>
</head>
<body>
<div class="container">
@Html.Raw(@ViewBag.MyHtmlString)
</div>
</body>
</html>
控制器:
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
var s = "link for HttpUtility.htmldecode conversion https://docs.microsoft.com/en-us/dotnet/api/system.web.httputility.htmldecode?view=netframework-4.8 <script> Hello World </script>";
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
ViewBag.MyHtmlString = r.Replace(s, "<a href=\"$1\" target=\"_blank\">$1</a>").Replace("href=\"www", "href=\"http://www");
return View(new SampleViewModel());
}
}
}
我需要将链接显示为可链接,并将脚本显示为内容。