我具有以下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 Core ToHtmlString,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50877875/