首页 文章

ASP .NET核心输入标记助手不使用Razor代码

提问于
浏览
6

我想将输入标记助手与剃刀代码组合起来设置属性,但我不能让这两种技术一起工作 . 我只是试图根据视图模型属性的值在输入字段上设置disabled属性 .

当我把剃刀代码放在 asp-for 标签后面时,无法识别剃刀智能感知并且字段未按预期禁用...

<input asp-for="OtherDrugs" @((Model.OtherDrugs == null) ? "disabled" : "") class="form-control" />

渲染输出......

<input type="text" id="OtherDrugs" name="OtherDrugs" value="" />

当我将razor代码放在 asp-for 标记之前时,标记帮助程序intellisense无法识别,并且该字段未按预期设置视图模型属性...

<input @((Model.OtherDrugs == null) ? "disabled" : "") asp-for="OtherDrug" class="form-control" />

渲染输出......

<input disabled asp-for="OtherDrugs" class="form-control" />

Note that combining tag helpers and razor does work if the razor code is inside a class attribute. Unfortunately input fields require the disabled attribute and not the disabled class for bootstrap 3.

有没有办法让这项工作?

3 回答

  • -1

    要呈现禁用的输入元素,只需添加禁用的属性即可 . 以下所有内容将呈现禁用的输入文本元素 .

    <input type="checkbox" disabled />
    <input type="checkbox" disabled="disabled" />
    <input type="checkbox" disabled="false" />
    <input type="checkbox" disabled="no" />
    <input type="checkbox" disabled="enabled" />
    <input type="checkbox" disabled="why is it still disabled" />
    

    在Asp.NET Core中,您可以扩展现有的输入标记帮助程序以创建只读输入标记帮助程序 .

    扩展 InputTagHelper 类,添加新属性以标识是否应禁用输入,并根据此值将"disabled"属性添加到输入 .

    [HtmlTargetElement("input", Attributes = ForAttributeName)]
    public class MyCustomTextArea : InputTagHelper
    {
        private const string ForAttributeName = "asp-for";
    
        [HtmlAttributeName("asp-is-disabled")]
        public bool IsDisabled { set; get; }
    
        public MyCustomTextArea(IHtmlGenerator generator) : base(generator)
        {
        }
    
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (IsDisabled)
            {
                var d = new TagHelperAttribute("disabled", "disabled");
                output.Attributes.Add(d);
            }
            base.Process(context, output);
        }
    }
    

    现在要使用这个自定义textarea助手,你需要在 _ViewImports.cshtml 中调用 addTagHelper 方法 .

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @addTagHelper *, YourAssemblyNameHere
    

    现在在您的视图中,您可以指定 asp-is-disabled 属性值 .

    <input type="text" asp-for="OtherDrugs" 
                                      asp-is-disabled="@Model.OtherDrugs==null"/>
    
  • 12

    您可以像这样使用ASP Core标记助手:

    <input asp-for="Name" />
    

    然后为您的 property 添加[可编辑(虚假)],如下所示:

    [Editable(false)] public string Name {set;get;}

    那么你应该扩展InputTagHelper:

    [HtmlTargetElement("input", Attributes = ForAttributeName)]
    public class ExtendedInputTagHelper : InputTagHelper
    {
        private const string ForAttributeName = "asp-for";
    
        public ExtendedInputTagHelper(IHtmlGenerator generator)
            : base(generator) { }
    
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var isContentModified = output.IsContentModified;
    
            if (For.Metadata.IsReadOnly)
            {
                var attribute = new TagHelperAttribute("disabled", "disabled");
                output.Attributes.Add(attribute);
            }
    
            if (!isContentModified)
            {
                base.Process(context, output);
            }
        }
    }
    

    最后在_ViewImports.cshtml中导入TagHelper:

    @addTagHelper *, <your assembly name>
    

    该解决方案的优点是将逻辑放在模型中并保留MVC原则 .

  • 1

    你最好做这样的事情:

    @{
        var isDisabled = ((Model.OtherDrugs == null) ? "disabled" : string.Empty());
     }
    

    然后

    @Html.TextBox("OtherDrugs", "", new { isDisabled  })
    

相关问题