2016-11-08 56 views
4

我想將輸入標籤助手和剃鬚刀代碼結合起來設置屬性,但我無法使這兩種技術一起工作。我只是試圖根據view model屬性的值在輸入字段上設置disabled屬性。ASP .NET Core輸入標籤助手不能使用剃鬚刀代碼

當我把剃刀代碼asp-for標籤剃刀智能感知無法識別和後場無法按預期禁用...

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

渲染輸出...

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

當我把剃鬚刀代碼放在asp-for標籤之前時,標籤幫助器智能感知不被識別,並且該字段沒有按照預期設置視圖模型屬性...

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

渲染輸出...

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

注意結合標籤傭工和剃刀如果剃刀代碼是一個類屬性裏面確實工作。不幸的是,輸入字段需要禁用屬性,而不是禁用類的引導程序3.

有沒有辦法使這項工作?

+0

我還沒有使用TagHelpers,所以我在黑暗中拍攝這裏,你可以包裝三元在? – JB06

回答

9

要呈現禁用的輸入元素,只需添加禁用的屬性即可。以下所有將呈現禁用的輸入文本元素。

<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的幫手,你需要調用addTagHelper方法_ViewImports.cshtml

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

現在,在您看來,您可以指定asp-is-disabled屬性值。

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

會很好,因爲它可以開箱即用,但是您的自定義標籤助手很好地完成了這項技巧。謝謝 – OjM

+0

@Shyju我對輸入使用了這個自定義標籤助手,除了一個問題外,它工作得很好。它呈現一個複選框兩次。對此有何想法? – dherrin79

+0

這裏同樣的東西,它不會像它應該那樣工作。 – Marko

-1

你最好不要做這樣的事情:

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

然後

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

它不是TagHelper –

1

您可以使用ASP核心標籤助手這樣的:

<input asp-for="Name" /> 

然後將[Editable(false)]設置爲這樣的屬性:

[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); 
     } 
    } 
} 

,最後導入TagHelper在_ViewImports.cshtml:

@addTagHelper *, <your assembly name> 

這種解決方案的優點是把邏輯模型保留MVC原則。