什么是ASP.NET 5中的System.Web.Mvc.Html.InputExtensions等效项? [英] What is the equivalent of System.Web.Mvc.Html.InputExtensions in ASP.NET 5?
问题描述
与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;
可以找到源代码 查看全文