{ -6ren">
gpt4 book ai didi

c# - ASP.NET 核心 ToHtmlString

转载 作者:行者123 更新时间:2023-11-30 13:43:46 25 4
gpt4 key购买 nike

我有以下 Kendo UI 网格,我需要呈现详细信息页面的操作链接:

@(Html.Kendo().Grid<Model>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.Id).Hidden(true);

@* Invalid line of code as ClientTemplate is waiting for a string *@
columns.Bound(c => c.Name).ClientTemplate(Html.ActionLink("#=Name#", "Details", new { id = "#=Id#" }));
@* Invalid line of code as ClientTemplate is waiting for a string *@

columns.Bound(c => c.Type).Width(100);
columns.Bound(c => c.Subdomain).Width(150);
columns.Bound(c => c.Description);
columns.Bound(c => c.Status).Width(100);
columns.Select().Width(50);
})
.AutoBind(false)
.Scrollable()
.Pageable(pageable => pageable
.Refresh(false)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Read", "Data"))
.PageSize(5)).Deferred())

ClientTemplate 方法需要一个 html 字符串。

columns.Bound(c => c.Name).ClientTemplate(string template) 

在 .NET Core 之前,您将按以下方式处理此请求:

  columns.Bound(c => c.Name).ClientTemplate(Html.ActionLink("#=Name#", "Details", new { id = "#=Id#" }).ToHtmlString());

不幸的是 .ToHtmlString() ( https://msdn.microsoft.com/en-us/library/system.web.htmlstring.tohtmlstring(v=vs.110).aspx ) 是 System.Web dll 的一部分。

我们如何在 .NET Core 中处理这个问题?

最佳答案

我最终为 IHtmlContent 创建了一个扩展方法:

public static class HtmlContentExtensions
{
public static string ToHtmlString(this IHtmlContent htmlContent)
{
if (htmlContent is HtmlString htmlString)
{
return htmlString.Value;
}

using (var writer = new StringWriter())
{
htmlContent.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
return writer.ToString();
}
}
}

我在我的 Kendo UI 网格中按以下方式使用它:

columns.Bound(c => c.Name).ClientTemplate(Html.ActionLink("#=Name#", "Details", new { id = "#=Id#" }).ToHtmlString());

关于c# - ASP.NET 核心 ToHtmlString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50877875/

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