I am porting an old .Net 4.8 app to .Net core and the old app has custom code which extends the @Html.EditorFor to wrap the within a and appends a after the tag (basically to add a “%” sign after the input field). I have added the older code below.
public static MvcHtmlString Addon(this MvcHtmlString htmlText, string text, string classes)
{
var wrapper = new TagBuilder("div");
wrapper.AddCssClass("input-group");
wrapper.AddCssClass(classes);
wrapper.InnerHtml = htmlText.ToHtmlString();
var span = new TagBuilder("span");
span.AddCssClass("input-group-addon");
span.InnerHtml = text;
wrapper.InnerHtml += span.ToString();
return new MvcHtmlString(wrapper.ToString());
}
The above code is used in conjunction with the @Html.EditorFor as below.
@Html.EditorFor(model => model.PercentageOfControlledSubstances, new { htmlAttributes = new { @class = "form-control short-textfield percent" } }).**Addon**("%", "med-textfield")
When compiling I get the error an error stating -> “Reference to type ‘HtmlString’ claims it is defined in ‘System.Web’, but it could not be found”.
Below is a screenshot of what the final rendering should look like.
tag followed by
I know this is a conversion from 4.8 to .Net Core and I have been searching how to convert my code but cannot find documentation on how to do this in .Net Core. After a lot of searching it looks like .Net Core uses either IHtmlContent or HtmlHelper(s) for this type coding but I am not sure – since I can find no documentation.