[英]Taghelper attributes must be well formed
我在 .net 5.0 中使用 Razor 頁
我想使用錨標記助手來生成href
,如下所示
<a asp-page="/Add" class="ml-4 d-lg-block" @* something *@)"
當使用標簽助手添加任何@* something *@
代碼時,它會給出一個編譯錯誤
而且如果我使用任何條件來渲染標簽,它會拋出編譯錯誤
<a asp-page="/Edit" @(Model.Condition?"":"disabled")>
說編譯錯誤:taghelper 'a' must not have c# in the elemnts attribute declaration area
我如何使用 C# 和標簽助手來滿足上述兩個條件
這是您必須用於有條件地添加disabled
屬性的語法:
<a asp-page="/Edit" disabled="@Model.Condition">...</a>
當條件為真時,它將生成:
<a href="..." disabled="disabled">...</a>
當條件為假時,它將生成:
<a href="...">...</a>
請記住,錨元素沒有disabled
屬性,因此您必須添加額外的 CSS 來禁用它們:
a[disabled] {
pointer-events: none;
}
這個解決方案在這里有更詳細的解釋: https://stackoverflow.com/a/10276157/10839134
disabled
對標簽不起作用,你可以嘗試使用按鈕,並在 onclick 中添加window.location.href
:
@if(Model.Condition==""){
<button onclick="window.location.href='/Edit'" >...</button>
}else
{
<button onclick="window.location.href='/Edit'" disabled">...</button>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.