.NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() Error












1














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();


Now I am developing .net core and I get error at



div.InnerHtml += label.ToString();


Error Image




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.










share|improve this question





























    1














    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();


    Now I am developing .net core and I get error at



    div.InnerHtml += label.ToString();


    Error Image




    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.










    share|improve this question



























      1












      1








      1


      2





      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();


      Now I am developing .net core and I get error at



      div.InnerHtml += label.ToString();


      Error Image




      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.










      share|improve this question















      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();


      Now I am developing .net core and I get error at



      div.InnerHtml += label.ToString();


      Error Image




      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.







      c# .net asp.net-mvc .net-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 20:34







      user3559349

















      asked Nov 21 '18 at 14:08









      Haro MaroHaro Maro

      163




      163
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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();


          What you should do is using AppendHtml() against existing LabelFor helper:



          div.InnerHtml.AppendHtml(label);


          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) 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) 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);
          }


          Note: This example helper already tested in VS 2017, .NET Core 2.1.



          Related issue:



          Create Custom HTML Helper in ASP.Net Core






          share|improve this answer





















          • thank you for your answer, this solved my problem
            – Haro Maro
            Nov 27 '18 at 9:15



















          0














          Have you tried this ?:



          div.InnerHtml += label.ToHtmlString(); 





          share|improve this answer





















          • There is no such a method ToHtmlString() in my visualstudio >.<
            – Haro Maro
            Nov 21 '18 at 14:21










          • @HaroMaro You should implement System.Web.HtmlString
            – Sk83r1l4m4
            Nov 21 '18 at 14:22










          • Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
            – Sk83r1l4m4
            Nov 21 '18 at 14:24












          • Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
            – Haro Maro
            Nov 21 '18 at 14:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413899%2fnet-core-mvchtmlstringtextboxfor-labelfor-tostring-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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();


          What you should do is using AppendHtml() against existing LabelFor helper:



          div.InnerHtml.AppendHtml(label);


          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) 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) 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);
          }


          Note: This example helper already tested in VS 2017, .NET Core 2.1.



          Related issue:



          Create Custom HTML Helper in ASP.Net Core






          share|improve this answer





















          • thank you for your answer, this solved my problem
            – Haro Maro
            Nov 27 '18 at 9:15
















          0














          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();


          What you should do is using AppendHtml() against existing LabelFor helper:



          div.InnerHtml.AppendHtml(label);


          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) 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) 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);
          }


          Note: This example helper already tested in VS 2017, .NET Core 2.1.



          Related issue:



          Create Custom HTML Helper in ASP.Net Core






          share|improve this answer





















          • thank you for your answer, this solved my problem
            – Haro Maro
            Nov 27 '18 at 9:15














          0












          0








          0






          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();


          What you should do is using AppendHtml() against existing LabelFor helper:



          div.InnerHtml.AppendHtml(label);


          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) 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) 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);
          }


          Note: This example helper already tested in VS 2017, .NET Core 2.1.



          Related issue:



          Create Custom HTML Helper in ASP.Net Core






          share|improve this answer












          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();


          What you should do is using AppendHtml() against existing LabelFor helper:



          div.InnerHtml.AppendHtml(label);


          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) 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) 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);
          }


          Note: This example helper already tested in VS 2017, .NET Core 2.1.



          Related issue:



          Create Custom HTML Helper in ASP.Net Core







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 1:55









          Tetsuya YamamotoTetsuya Yamamoto

          14.7k41939




          14.7k41939












          • thank you for your answer, this solved my problem
            – Haro Maro
            Nov 27 '18 at 9:15


















          • thank you for your answer, this solved my problem
            – Haro Maro
            Nov 27 '18 at 9:15
















          thank you for your answer, this solved my problem
          – Haro Maro
          Nov 27 '18 at 9:15




          thank you for your answer, this solved my problem
          – Haro Maro
          Nov 27 '18 at 9:15













          0














          Have you tried this ?:



          div.InnerHtml += label.ToHtmlString(); 





          share|improve this answer





















          • There is no such a method ToHtmlString() in my visualstudio >.<
            – Haro Maro
            Nov 21 '18 at 14:21










          • @HaroMaro You should implement System.Web.HtmlString
            – Sk83r1l4m4
            Nov 21 '18 at 14:22










          • Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
            – Sk83r1l4m4
            Nov 21 '18 at 14:24












          • Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
            – Haro Maro
            Nov 21 '18 at 14:52
















          0














          Have you tried this ?:



          div.InnerHtml += label.ToHtmlString(); 





          share|improve this answer





















          • There is no such a method ToHtmlString() in my visualstudio >.<
            – Haro Maro
            Nov 21 '18 at 14:21










          • @HaroMaro You should implement System.Web.HtmlString
            – Sk83r1l4m4
            Nov 21 '18 at 14:22










          • Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
            – Sk83r1l4m4
            Nov 21 '18 at 14:24












          • Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
            – Haro Maro
            Nov 21 '18 at 14:52














          0












          0








          0






          Have you tried this ?:



          div.InnerHtml += label.ToHtmlString(); 





          share|improve this answer












          Have you tried this ?:



          div.InnerHtml += label.ToHtmlString(); 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:17









          Sk83r1l4m4Sk83r1l4m4

          418




          418












          • There is no such a method ToHtmlString() in my visualstudio >.<
            – Haro Maro
            Nov 21 '18 at 14:21










          • @HaroMaro You should implement System.Web.HtmlString
            – Sk83r1l4m4
            Nov 21 '18 at 14:22










          • Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
            – Sk83r1l4m4
            Nov 21 '18 at 14:24












          • Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
            – Haro Maro
            Nov 21 '18 at 14:52


















          • There is no such a method ToHtmlString() in my visualstudio >.<
            – Haro Maro
            Nov 21 '18 at 14:21










          • @HaroMaro You should implement System.Web.HtmlString
            – Sk83r1l4m4
            Nov 21 '18 at 14:22










          • Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
            – Sk83r1l4m4
            Nov 21 '18 at 14:24












          • Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
            – Haro Maro
            Nov 21 '18 at 14:52
















          There is no such a method ToHtmlString() in my visualstudio >.<
          – Haro Maro
          Nov 21 '18 at 14:21




          There is no such a method ToHtmlString() in my visualstudio >.<
          – Haro Maro
          Nov 21 '18 at 14:21












          @HaroMaro You should implement System.Web.HtmlString
          – Sk83r1l4m4
          Nov 21 '18 at 14:22




          @HaroMaro You should implement System.Web.HtmlString
          – Sk83r1l4m4
          Nov 21 '18 at 14:22












          Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
          – Sk83r1l4m4
          Nov 21 '18 at 14:24






          Also I found this on another forum HtmlString str = new HtmlString(label.value); Try it as well and let me know if this did the trick.
          – Sk83r1l4m4
          Nov 21 '18 at 14:24














          Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
          – Haro Maro
          Nov 21 '18 at 14:52




          Thank you for your interest but dont work these. label has only .GetType, .Equals .GetHashCode methods :(
          – Haro Maro
          Nov 21 '18 at 14:52


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413899%2fnet-core-mvchtmlstringtextboxfor-labelfor-tostring-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen