.NET CORE MvcHtmlString(TextboxFor,LabelFor).ToString()错误 [英] .NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() Error

查看:53
本文介绍了.NET CORE MvcHtmlString(TextboxFor,LabelFor).ToString()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我必须将MvcHtmlString转换为字符串.我在此代码之前使用过.(.net框架)

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,版本= 4.0.0.0,文化=中性,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆