ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等价物是什么?
ASP.NET 4中使用的ASP.NET 5等效的System.Web.Mvc.Html.InputExtensions
是什么?
见下面的例子:
public static class CustomHelpers { // Submit Button Helper public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText) { string str = ""; return new MvcHtmlString(str); } // Readonly Strongly-Typed TextBox Helper public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression<Func> 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代码:
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;
源代码可以在这里找到。
上述就是C#学习教程:ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等价物是什么?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/954542.html