.NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() 错误 [英] .NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() Error
问题描述
您好,我必须将 MvcHtmlString 转换为字符串.我之前用过这段代码.(.net framework)
Hello i must convert MvcHtmlString to string. I was using before this code.(.net framework)
TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "form-group");
var label = helper.LabelFor(expression, new { @class = "control-label col-lg-1" });
div.InnerHtml += label.ToString();
现在我正在开发 .net 核心,但在
Now I am developing .net core and I get error at
div.InnerHtml += label.ToString();
返回代表当前对象的字符串.对类型HtmlString"的引用声称它在System.Web"中定义,但无法找到.模块'System.Web, Version=4.0.0.0, Culture=neutral, PublicyToken=b03f5f7f11d50a3a 应该被引用
Returns a string that represent the current object. Reference to type 'HtmlString' claims it is defined in 'System.Web', but it could not be found. Module 'System.Web, Version=4.0.0.0, Culture=neutral, PublicyToken=b03f5f7f11d50a3a should be referenced
请帮帮我.我必须使用这种方法或替代方法.我正在尝试构建一个表单.
Please help me. I must use this method or alternative. I am trying build a form.
推荐答案
下面这行导致错误,因为 TagBuilder.InnerHtml
是只读属性,您不能使用 +=
运算符来分配 HTML 字符串:
This line below causing error because TagBuilder.InnerHtml
is a read only property and you can't use +=
operator to assign HTML string:
div.InnerHtml += label.ToString();
你应该做的是对现有的 LabelFor
助手使用 AppendHtml()
:
What you should do is using AppendHtml()
against existing LabelFor
helper:
div.InnerHtml.AppendHtml(label);
请注意,.NET Core MVC 不使用 System.Web
命名空间,而是使用 Microsoft.AspNetCore
父命名空间.您应该按照以下步骤尝试 IHtmlContent
来构建您自己的自定义助手而不是 MvcHtmlString
:
Take note that .NET Core MVC doesn't use System.Web
namespace, it uses Microsoft.AspNetCore
parent namespace instead. You should try IHtmlContent
to build your own custom helper instead of MvcHtmlString
, by following these steps:
1) 包含下面提供的所有 using
语句以启用 IHtmlContent
和 IHtmlHelper
实例.
1) Include all using
statements provided below to enable IHtmlContent
and IHtmlHelper
instance.
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
2) 使用 Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper
作为参数而不是 System.Web.Mvc.HtmlHelper
创建自定义助手,如下例所示:
2) Create your custom helper using Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper
as parameter instead of System.Web.Mvc.HtmlHelper
, shown in example below:
public static IHtmlContent CustomLabelFor<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
string result;
TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "form-group");
var label = helper.LabelFor(expression, new { @class = "control-label col-lg-1" });
div.InnerHtml.AppendHtml(label);
using (var sw = new System.IO.StringWriter())
{
div.WriteTo(sw, System.Text.Encodings.Web.HtmlEncoder.Default);
result = sw.ToString();
}
return new HtmlString(result);
}
注意:此示例助手已在 VS 2017、.NET Core 2.1 中进行了测试.
Note: This example helper already tested in VS 2017, .NET Core 2.1.
相关问题:
在 ASP.Net Core 中创建自定义 HTML 助手一个>
这篇关于.NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!