指的是ASP.NET MVC视图的`HtmlHelper`类。
用disabled创建SelectListItem =“ disabled”属性
我没有看到通过the the the创建的方法,它会吐出以下html:
我正在尝试编写一种用于HtmlHelper的扩展方法。这是一个修改的版本HtmlHelper.Raw(),看起来如下。 /// <summary> /// Wraps HTML markup in an IHtmlString, which will enable HTML markup to be /// rendered to the output without getting HTML encoded. /// </summary> /// <param name="value">HTML markup string.</param> /// <returns>An IHtmlString that represents HTML markup.</returns> [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "For consistency, all helpers are instance methods.")] public IHtmlString Raw(string value) { return new HtmlString(value); } ,我尝试编写以下内容。 using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using System.Web; public static class HtmlHelperExtensions { public static IHtmlString Text(this IHtmlHelper helper, string text) { return new HtmlString(TextToHtml.Transform(text)); } } 但return语句给我一个错误。 Cannot隐式将类型“ Microsoft.aspnetcore.html.htmlstring”转换为“ System.Web.ihtmlString”。存在明确的转换(您是否缺少演员?) 我不清楚为什么Microsoft的代码有效,但我的代码却没有。 TextToHtml.Transform()返回常规字符串。有人知道如何将其转换为IHtmlString? update 我尝试了一个简单的演员,但这给了我一个运行时错误。 System.invalidcastException:“无法施放类型'Microsoft.aspnetcore.html.htmlString'的对象' 我不确定为什么Microsoft的代码可以工作,而我的代码不确定,但是如果我的扩展方法返回IHtmlContent而不是IHtmlString,则看起来ASP.NET Core同样高兴。 HtmlString已经实现了IHtmlContent。
我使用 TagBuilder 返回 HTML,发生的情况是 HTML 呈现为文本,这是代码的一部分: 私有字符串 RenderAlert() { // 变种 我正在使用 TagBuilder 返回 HTML,发生的情况是 HTML 呈现为文本,这是代码的一部分: private string RenderAlert() { //<div class="alert-box"> var wrapper = new TagBuilder("div"); //merge attributes wrapper.MergeAttributes(htmlAttributes != null ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) : null); if (alertStyle != AlertStyle.Default) wrapper.AddCssClass(alertStyle.ToString().ToLower()); wrapper.AddCssClass("alert-box"); //build html wrapper.InnerHtml.AppendHtml(text); //Add close button if (!hideCloseButton) wrapper.InnerHtml.AppendHtml(RenderCloseButton()); var stringWriter = new System.IO.StringWriter(); wrapper.WriteTo(stringWriter, HtmlEncoder.Default); var tagBuilderIsFinallyAStringNow = stringWriter.ToString(); return tagBuilderIsFinallyAStringNow; } private static string RenderCloseButton() { //<a href="" class="close">x</a> var closeButton = new TagBuilder("a"); closeButton.AddCssClass("close"); closeButton.Attributes.Add("href", ""); closeButton.InnerHtml.AppendHtml("x"); var stringWriter = new System.IO.StringWriter(); closeButton.WriteTo(stringWriter, HtmlEncoder.Default); var tagBuilderIsFinallyAStringNow = stringWriter.ToString(); return tagBuilderIsFinallyAStringNow; } public override string ToString() { return RenderAlert(); <-- this is where the html finally gets rendered } 这是 HTML 页面: @using MyHelpers; @{ ViewBag.Title = "Alert Box Examples"; } <h2>@ViewBag.Title</h2> <h3>The markup</h3> <div class="alert-box"> This is a standard alert (div.alert-box). <a href="" class="close">×</a> </div> <div class="alert-box success"> This is a success alert (div.alert-box.success). <a href="" class="close">×</a> </div> <div class="alert-box warning"> This is an warning (div.alert-box.warning). <a href="" class="close">×</a> </div> <div class="alert-box info"> This is a info alert (div.alert-box.info). <a href="" class="close">×</a> </div> <h3>Fluent API</h3> <span>Html.Alert("Message")</span> @Html.Alert("Message") <span>Html.Alert("Message").Success()</span> @Html.Alert("Message").Success() <span>Html.Alert("Message").Warning()</span> @Html.Alert("Message").Warning() <span>Html.Alert("Message").Info()</span> @Html.Alert("Message").Info() <span>Html.Alert("No close button").Info().HideCloseButton().Attributes(new { data_special = "SpecialAttribute" })</span> @Html.Alert("No close button").Info().HideCloseButton().Attributes(new { data_special = "SpecialAttribute" }) <h4>Standard API</h4> <span>Html.Alert("Another message", hideCloseButton:true)</span> @Html.Alert("Another message", hideCloseButton: true) @section scripts { <script> //On Page Load (function ($) { $(function () { //Attach plugins //Alerts plugin $(document).foundationAlerts(); }); })(jQuery); </script> } 但是我没有得到原始 HTML,而是渲染了文本: Alert Box Examples The markup This is a standard alert (div.alert-box).× This is a success alert (div.alert-box.success).× This is an warning (div.alert-box.warning).× This is a info alert (div.alert-box.info).× Fluent API Html.Alert("Message") <div class="alert-box">Message<a class="close" href="">x</a></div> Html.Alert("Message").Success() <div class="success alert-box">Message<a class="close" href="">x</a></div> Html.Alert("Message").Warning() <div class="warning alert-box">Message<a class="close" href="">x</a></div> Html.Alert("Message").Info() <div class="info alert-box">Message<a class="close" href="">x</a></div> Html.Alert("No close button").Info().HideCloseButton().Attributes(new { data_special = "SpecialAttribute" }) <div class="info alert-box" data-special="SpecialAttribute">No close button</div> Standard API Html.Alert("Another message", hideCloseButton:true) <div class="alert-box">Another message</div> using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures; using System.Text.Encodings.Web; namespace MyHelpers { public class AlertBox : IAlertBox { private readonly string text; private AlertStyle alertStyle; private bool hideCloseButton; private object htmlAttributes; /// <summary> /// Returns a div alert box element with the options specified /// </summary> /// <param name="text">Sets the text to display</param> /// <param name="style">Sets style of alert box [Default | Success | Warning | Info ]</param> /// <param name="hideCloseButton">Sets the close button visibility</param> /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param> public AlertBox(string text, AlertStyle style, bool hideCloseButton = false, object htmlAttributes = null) { this.text = text; this.alertStyle = style; this.hideCloseButton = hideCloseButton; this.htmlAttributes = htmlAttributes; } #region FluentAPI /// <summary> /// Sets the display style to Success /// </summary> public IAlertBoxFluentOptions Success() { alertStyle = AlertStyle.Success; return new AlertBoxFluentOptions(this); } /// <summary> /// Sets the display style to Warning /// </summary> /// <returns></returns> public IAlertBoxFluentOptions Warning() { alertStyle = AlertStyle.Warning; return new AlertBoxFluentOptions(this); } /// <summary> /// Sets the display style to Info /// </summary> /// <returns></returns> public IAlertBoxFluentOptions Info() { alertStyle = AlertStyle.Info; return new AlertBoxFluentOptions(this); } /// <summary> /// Sets the close button visibility /// </summary> /// <returns></returns> public IAlertBoxFluentOptions HideCloseButton(bool hideCloseButton = true) { this.hideCloseButton = hideCloseButton; return new AlertBoxFluentOptions(this); } /// <summary> /// An object that contains the HTML attributes to set for the element. /// </summary> /// <param name="htmlAttributes"></param> /// <returns></returns> public IAlertBoxFluentOptions Attributes(object htmlAttributes) { this.htmlAttributes = htmlAttributes; return new AlertBoxFluentOptions(this); } #endregion //FluentAPI private string RenderAlert() { //<div class="alert-box"> var wrapper = new TagBuilder("div"); //merge attributes wrapper.MergeAttributes(htmlAttributes != null ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) : null); if (alertStyle != AlertStyle.Default) wrapper.AddCssClass(alertStyle.ToString().ToLower()); wrapper.AddCssClass("alert-box"); //build html wrapper.InnerHtml.AppendHtml(text); //Add close button if (!hideCloseButton) wrapper.InnerHtml.AppendHtml(RenderCloseButton()); var stringWriter = new System.IO.StringWriter(); wrapper.WriteTo(stringWriter, HtmlEncoder.Default); var tagBuilderIsFinallyAStringNow = stringWriter.ToString(); return tagBuilderIsFinallyAStringNow; } private static string RenderCloseButton() { //<a href="" class="close">x</a> var closeButton = new TagBuilder("a"); closeButton.AddCssClass("close"); closeButton.Attributes.Add("href", ""); closeButton.InnerHtml.AppendHtml("x"); var stringWriter = new System.IO.StringWriter(); closeButton.WriteTo(stringWriter, HtmlEncoder.Default); var tagBuilderIsFinallyAStringNow = stringWriter.ToString(); return tagBuilderIsFinallyAStringNow; } //Render HTML public override string ToString() { return RenderAlert(); } //Return ToString public string ToHtmlString() { return ToString(); } } } 这是 HtmlHelper: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; namespace MyHelpers { /// <summary> /// Generates an Alert message /// </summary> public static class AlertHtmlHelper { /// <summary> /// Generates an Alert message /// </summary> public static AlertBox Alert(this IHtmlHelper html, string text, AlertStyle alertStyle = AlertStyle.Default, bool hideCloseButton = false, object htmlAttributes = null ) { return new AlertBox(text, alertStyle, hideCloseButton, htmlAttributes); } // Strongly typed /// <summary> /// Generates an Alert message /// </summary> public static AlertBox AlertFor<TModel, TTextProperty>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TTextProperty>> expression, AlertStyle alertStyle = AlertStyle.Default, bool hideCloseButton = false, object htmlAttributes = null ) { var metadata = ExpressionMetadataProvider.FromLambdaExpression(expression, html.ViewData, html.MetadataProvider); return new AlertBox((string)metadata.Model, alertStyle, hideCloseButton, htmlAttributes); } /// <summary> /// Generates an Alert message /// </summary> public static AlertBox AlertFor<TModel, TTextProperty, TStyleProperty>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TTextProperty>> textExpression, Expression<Func<TModel, TStyleProperty>> styleExpression, bool hideCloseButton = false, object htmlAttributes = null ) { var text = (string)ExpressionMetadataProvider.FromLambdaExpression(textExpression, html.ViewData, html.MetadataProvider).Model; var alertStyle = (AlertStyle)ExpressionMetadataProvider.FromLambdaExpression(styleExpression, html.ViewData, html.MetadataProvider).Model; return new AlertBox(text, alertStyle,hideCloseButton, htmlAttributes); } } } AlertBoxFluentOptions.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace MyHelpers { public class AlertBoxFluentOptions : IHtmlString, IAlertBoxFluentOptions { private readonly AlertBox parent; public AlertBoxFluentOptions(AlertBox parent) { this.parent = parent; } public IAlertBoxFluentOptions HideCloseButton(bool hideCloseButton = true) { return parent.HideCloseButton(hideCloseButton); } public IAlertBoxFluentOptions Attributes(object htmlAttributes) { return parent.Attributes(htmlAttributes); } public override string ToString() { return parent.ToString(); } public string ToHtmlString() { return ToString(); } } } AlertStyle.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyHelpers { public enum AlertStyle { Default, Success, Warning, Info } } IAertBox.cs: using System.Web; namespace MyHelpers { public interface IAlertBox : IHtmlString, IAlertBoxFluentOptions { IAlertBoxFluentOptions Success(); IAlertBoxFluentOptions Warning(); IAlertBoxFluentOptions Info(); } } IAertBoxFluentOptions.cs: using System.Web; namespace MyHelpers { public interface IAlertBoxFluentOptions : IHtmlString { IAlertBoxFluentOptions HideCloseButton(bool hideCloseButton = true); IAlertBoxFluentOptions Attributes(object htmlAttributes); } } 我在 GitHub 上使用了这个示例: 由于该项目已经过时,我不得不进行一些更改。 https://github.com/EdCharbeneau/FluentHtmlHelpers/tree/master/FluentHtmlHelpers 参考内置HTML Helper(HtmlHelperInputExtensions.TextBoxFor方法),返回类型为IHtmlContent。 想法是一样的,要渲染 HTML,您需要使用 IHtmlContent 而不是 string 类型的值。 using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; public class AlertBox : IAlertBox { ... //Return ToString public IHtmlContent ToHtmlString() { return new HtmlString(ToString()); } } 在您的 AlertBoxFluentOptions 中,您应该有一个返回 IHtmlContent 值的方法。 public interface IAlertBoxFluentOptions { IHtmlContent ToHtmlString(); } public class AlertBoxFluentOptions : IAlertBoxFluentOptions { ... public IHtmlContent ToHtmlString() { return new HtmlString(ToString()); } } 在您看来,您应该为 HTML 帮助器方法调用 ToHtmlString 方法。 @Html.Alert("Message").ToHtmlString() @Html.Alert("Message").Success().ToHtmlString() 如果不在视图中调用.ToHtmlString(),两个类都应该实现IHtmlContent。 public class AlertBox : IAlertBox, IHtmlContent { ... public void WriteTo(TextWriter writer, HtmlEncoder encoder) { writer.Write(ToString()); } } public class AlertBoxFluentOptions : IAlertBoxFluentOptions, IHtmlContent { ... public void WriteTo(TextWriter writer, HtmlEncoder encoder) { writer.Write(ToString()); } } 在视图中, @Html.Alert("Message") @Html.Alert("Message").Success()
我的字符串是: string title = "钻石中的CaSiO3钙钛矿表明洋壳循环进入下地幔。" 我使用@Html.Raw(标题...
如何在razor中的@Html.RadioButtonFor中添加aria-label?
在我的 MVC 剃刀视图中,我使用 @Html.RadioButtonFor。我想使用 aria-label 作为屏幕阅读器,但不知道如何使用它。 如果我添加 aria-label 那么我会得到 The name 'aria' does not believe in current
证明我错了:VB.NET HtmlHelper 扩展方法无法在 MVC 4 和 VS 2012 中工作
无论我尝试多少次,我都无法让我的 HTML Helper 扩展方法发挥作用。 这是我创建的测试步骤,如果有人想自己尝试一下: 使用 Visual Studio 2012,我...
ASP .NET Core - 扩展 @Html.EditorFor 来追加 <span>
我正在将旧的 .NET 4.8 应用程序移植到 .NET Core,旧应用程序具有自定义代码,该代码扩展了 @Html.EditorFor 以将 包装在 a 中,并在 ... 之后附加
在使用网站检查我的代码后,它显示我的所有代码都是正确的,并且根本没有任何错误,并且还使用 vscode 中的实时服务器扩展确认了它。 所以...
使用网站检查我的代码后,它显示我的所有代码都是正确的,并且根本没有任何错误,并且还使用 vscode 中的实时服务器扩展确认了它。所以...
@Html.LabelFor(LoginVM => model.UserName) @Html.TextBoxFor(模型=>模型.用户名) @Html.ValidationMessageFor(模型...
哪个更好,使用 Sass mixin 并使用 @include 包含它或定义一个辅助类并在 Html 中您需要的地方使用?例如我需要使用 {display:flex;调整内容:中心;...
CakePHP 2.0:意外的助手行为 (HtmlHelper::link)
我正在开发 CakePHP 2.0,并且想要制作一个语言助手,这样我就不必在我的观点中传递一些基于语言的东西。为此我创建了一个 LanguageHelper。 我的第一个任务是包括...
我希望能够过滤此下拉列表,使其仅显示集合中 MemoId 为 0 的项目 到目前为止我所做的是: @Html.DropDownListFor(模型 => 模型。
Cakephp 2.0:使用 Html->script() 时 Htmlhelper 抛出 array_merge() 错误
我使用以下代码来实现一些 jquery-ui 功能: Html->脚本(数组( 'jquery-ui-1.8.16.custom.min.js', ...
我已经使用这个 HTML Helper 一段时间了,遵循了理论上实现相同最终结果的各种示例。但是,我似乎无法产生最终结果。 也许有人...
我在 ASP.NET Core 8 MVC 视图中有以下代码: @{ var listaAreas = new List(); if (Model.Areas!= nul... 我在 ASP.NET Core 8 MVC 视图中有此代码: <div class="mb-3 position-relative"> @{ var listaAreas = new List<SelectListItem>(); if (Model.Areas != null && Model.Areas.Any()) { var areasActuales = Model.Areas.Select(r => new SelectListItem() { Text = r.Area?.AreaNombre ?? string.Empty, Value = (r.Area?.AreaId ?? 0).ToString(), Selected = r.Selected }); listaAreas.AddRange(areasActuales); } } @Html.LabelFor(m => m.Areas) @Html.ListBoxFor(m => m.Areas, listaAreas, new { @class = "form-control", style = "width: 100%", multiple = "multiple" }) </div> 调用 listaAreas 时,这是 ListBoxFor 数组: 即使唯一的选项有 Selected = true,HTML SELECT 标签也显示为未选中。 这是怎么回事? 编辑: 我最后一次尝试,但没有成功: <div class="mb-3 position-relative"> @{ MultiSelectList? listaAreas = null; if (Model.Areas != null && Model.Areas.Any()) { listaAreas = new MultiSelectList(Model.Areas.Where(a => a.Area != null).Select(a => a.Area), "AreaId", "AreaNombre", Model.Areas.Where(a => a.Selected).Select(a => a.Area?.AreaId).ToArray()); } } @Html.LabelFor(m => m.Areas) @Html.ListBoxFor(m => m.Areas, listaAreas, new { @class = "form-control", style = "width: 100%", multiple = "multiple" }) </div> 即使唯一的选项 Selected = true,也会出现 HTML SELECT 标签 没有选择。我已经删除了该属性,但同样的情况发生了 根据您共享的代码片段和描述,我已尝试调查您的问题。但是,在 ASP.NET Core 中,没有预定义的标记帮助器来显示多选下拉列表。如果您想实现带复选框的下拉菜单或 onclick 保持下拉框中的项目处于选中状态,您应该使用基于 javaScript 的插件。 我尝试使用 bootstrap-multiselect.js 来演示带有复选框的多选下拉列表。 让我们看看如何在实践中实现这一点。 控制器: public IActionResult MultiSelectCheckboxDropdown() { var multiSelectList = new List<SelectListItem> { new SelectListItem { Value = "1", Text = "C#" }, new SelectListItem { Value = "2", Text = "JavaScript" }, new SelectListItem { Value = "3", Text = "Python" }, new SelectListItem { Value = "4", Text = "Java" }, new SelectListItem { Value = "5", Text = "C++" }, new SelectListItem { Value = "6", Text = "Swift" }, new SelectListItem { Value = "7", Text = "Go" }, new SelectListItem { Value = "8", Text = "Ruby" }, new SelectListItem { Value = "9", Text = "TypeScript" }, new SelectListItem { Value = "10", Text = "PHP" } }; ViewBag.multiSelectList = multiSelectList; return View(); } 查看: <h3>Multi select</h3> @Html.DropDownList("Category", ViewBag.multiSelectList, null, new { @class = "selectpicker", multiple = "multiple", id = "Subjects_dropdown" }) <!-- Latest compiled and minified CSS --> @section scripts { <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/1.1.2/js/bootstrap-multiselect.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/1.1.2/css/bootstrap-multiselect.min.css"></script> <script> $(document).ready(function () { $('#myMultiSelect').select2(); $('#Subjects_dropdown').multiselect(); }); </script> } 输出: 注意: 请根据您的需要修改脚本和html。由于没有用于多选下拉列表的内置标签帮助器,所以我尝试与您分享这一点。如果您想检查更多样本,您可以查看此文档。
致命错误:未捕获类型错误:mysqli_stmt_bind_param():参数#1
致命错误:未捕获类型错误:mysqli_stmt_bind_param():参数#1 ($statement) 必须是 mysqli_stmt 类型,bool 给出 C:\xpp\htdocs\online-shopping-system-advanced dmin dmin ccept_produ...
尝试使用openai api在html网页中实现chatgpt
嘿,伙计们,我正在尝试使用一个 python 脚本来调用 openai api 来从 chatgpt 获取响应,因为它目前代表 python 脚本工作并正在终端中显示对话...
我需要在页面两侧添加两条黑线,但其中一部分被切断并且没有到达页脚 最大宽度:85,25em; 保证金:0 自动; 填充:1...
我想根据 asp.net MVC 中 Html.TextBoxFor 的条件设置禁用属性,如下所示 @Html.TextBoxFor(model => model.ExpireDate, new { style = "width: 70px;", maxlength = "10", id...