C# ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等效项是什么?
ASP.NET5与ASP.NET4中使用的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 = "&
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
命名空间。