什么是ASP.NET 5中的System.Web.Mvc.Html.InputExtensions等效项? [英] What is the equivalent of System.Web.Mvc.Html.InputExtensions in ASP.NET 5?

查看:216
本文介绍了什么是ASP.NET 5中的System.Web.Mvc.Html.InputExtensions等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与ASP.NET 4中使用的System.Web.Mvc.Html.InputExtensions等效的ASP.NET 5是什么?

What is the ASP.NET 5 equivalent of System.Web.Mvc.Html.InputExtensions as used in ASP.NET 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.NET 4代码:

For the ASP.NET 4 code:

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

ASP.NET 5的等效项是:

The ASP.NET 5 equivalent is:

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

或页面中包含的命名空间

or with the namespace included to your page

@Microsoft.AspNet.Mvc.Rendering;

它显示为:

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

请注意,其返回类型是接口IHtmlContent,而不是ASP.NET 4中的MvcHtmlString.

Take note its return type is an interface IHtmlContent and not a MvcHtmlString as in ASP.NET 4.

MvcHtmlString已在ASP.NET 5中替换为HtmlString.

MvcHtmlString has been replaced with HtmlString in ASP.NET 5.

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

Since an interface IHtmlContent of HtmlString is returned and not HtmlString itself you have to cast the return to HtmlString

但是您想将此作为ASP.NET 5中的扩展方法 因此您应该将方法返回类型更改为IHtmlContent,并将代码更改为:

However you want to use this as an extension method in ASP.NET 5 so you should change your method return type to IHtmlContent and your code to:

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

可以找到源代码 查看全文

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