What is equivalent to System.Web.Mvc.Html.InputExtensions in ASP.NET 5?

What is the ASP.NET 5 equivalent System.Web.Mvc.Html.InputExtensionsused in ASP.NET 4?

See an example below:

public static class CustomHelpers
{
    // Submit Button Helper
    public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText)
    {
        string str = "<input type=\"submit\" value=\"" + buttonText + "\" />";
        return new MvcHtmlString(str);
    }

    // Readonly Strongly-Typed TextBox Helper
    public static MvcHtmlString TextBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, bool isReadonly)
    {
        MvcHtmlString html = default(MvcHtmlString);

        if (isReadonly)
        {
            html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,
                expression, new { @class = "readOnly",
                @readonly = "read-only" });
        }
        else
        {
            html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression);
        }
        return html;
    }
}
+4
source share
1 answer

For ASP.NET 4 code:

    MvcHtmlString html = 
        System.Web.Mvc.Html.InputExtensions.TextBoxFor(
            htmlHelper, expression);

ASP.NET 5 equivalent:

Microsoft.AspNet.Mvc.Rendering.HtmlString html = 
     (Microsoft.AspNet.Mvc.Rendering.HtmlString)    
         Microsoft.AspNet.Mvc.Rendering.HtmlHelperInputExtensions.TextBoxFor(
             htmlHelper, expression);

or with the namespace included on your page

@Microsoft.AspNet.Mvc.Rendering;

he reads:

HtmlString html = (HtmlString)HtmlHelperInputExtensions.TextBoxFor(htmlHelper,expression);

Note that its return type is an interface IHtmlContent, and not MvcHtmlStringlike in ASP.NET 4.

MvcHtmlStringhas been replaced by HtmlStringin ASP.NET 5.

Since the interface is returned IHtmlContentfrom HtmlString, rather than HtmlString, you need to give an answer toHtmlString

ASP.NET 5 IHtmlContent :

 IHtmlContent html = HtmlHelperInputExtensions.TextBoxFor(htmlHelper,
                                          expression);
 return html;

.

+3

Source: https://habr.com/ru/post/1613303/


All Articles