在ASP中与System.Web.Mvc.Html.InputExtensions等价的是什么?净5

本文关键字:是什么 InputExtensions 中与 ASP System Web Html Mvc | 更新日期: 2023-09-27 18:19:19

什么是ASP。. NET 5相当于在ASP中使用的System.Web.Mvc.Html.InputExtensions。净4 ?

请看下面的例子:

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

在ASP中与System.Web.Mvc.Html.InputExtensions等价的是什么?净5

. NET 4代码:

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

ASP。相当于。NET 5:

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

或包含在页面

中的名称空间
@Microsoft.AspNet.Mvc.Rendering;

:

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

注意它的返回类型是一个接口IHtmlContent,而不是ASP中的MvcHtmlString。4 .

净ASP中

MvcHtmlString已被HtmlString取代。净5。

由于返回的是HtmlString的接口IHtmlContent而不是HtmlString本身,因此必须将返回值强制转换为HtmlString

但是你想在ASP中使用这个扩展方法。网5所以你应该改变你的方法返回类型为IHtmlContent和你的代码为:

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

源代码可以在这里找到