Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等效项是什么?_C#_Html Helper_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等效项是什么?

C# ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等效项是什么?,c#,html-helper,asp.net-core,asp.net-core-mvc,C#,Html Helper,Asp.net Core,Asp.net Core Mvc,ASP.NET5与ASP.NET4中使用的System.Web.Mvc.Html.InputExtensions的等价物是什么 见下例: public static class CustomHelpers { // Submit Button Helper public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText) { string str = "&

ASP.NET5与ASP.NET4中使用的
System.Web.Mvc.Html.InputExtensions
的等价物是什么

见下例:

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;
    }
}
公共静态类CustomHelpers
{
//提交按钮助手
公共静态MvcHtmlString SubmitButton(此HtmlHelper帮助程序,字符串buttonText)
{
字符串str=“”;
返回新的MvcHtmlString(str);
}
//只读强类型文本框帮助程序
公共静态MvcHtmlString TextBoxFor(此HtmlHelper HtmlHelper,表达式,bool isReadonly)
{
MvcHtmlString html=默认值(MvcHtmlString);
如果(仅限isReadonly)
{
html=System.Web.Mvc.html.InputExtensions.TextBoxFor(htmlHelper,
表达式,新的{@class=“readOnly”,
@readonly=“只读”});
}
其他的
{
html=System.Web.Mvc.html.InputExtensions.TextBoxFor(htmlHelper,表达式);
}
返回html;
}
}

对于ASP.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.NET 4中的
MvcHtmlString

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

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

但是,您希望在ASP.NET 5中将其用作扩展方法 因此,您应该将方法返回类型更改为
IHtmlContent
,将代码更改为:

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

可以找到源代码。

该方法是一个扩展方法,因此您可以这样调用它:
htmlhelp.TextBoxFor(expression)
。然后必须包含
Microsoft.AspNet.Mvc.Rendering
命名空间。