html標籤從.net framework轉移到.net standard(.net core 2.2)時遇到的坑及填坑

在原來的.net framework mvc中html的標籤能夠使用下面的方法html

 1 <select class="form-control" id="categoryinfoid" name="categoryinfoid">
 2                                     <option value="0">選擇</option>
 3                                     <option value="22" @(categoryinfoid == 22 ? "selected=selected" : "")>內雙</option>
 4                                     <option value="23" @(categoryinfoid == 23 ? "selected=selected" : "")>內三</option>
 5                                     <option value="24" @(categoryinfoid == 24 ? "selected=selected" : "")>內四</option>
 6                                     <option value="32" @(categoryinfoid == 32 ? "selected=selected" : "")>海雙</option>
 7                                     <option value="33" @(categoryinfoid == 33 ? "selected=selected" : "")>海三</option>
 8                                     <option value="34" @(categoryinfoid == 34 ? "selected=selected" : "")>海四</option>
 9                                     <option value="42" @(categoryinfoid == 42 ? "selected=selected" : "")>陽雙</option>
10                                     <option value="43" @(categoryinfoid == 43 ? "selected=selected" : "")>陽三</option>
11                                     <option value="44" @(categoryinfoid == 44 ? "selected=selected" : "")>陽四</option>
12                                     <option value="52" @(categoryinfoid == 52 ? "selected=selected" : "")>套雙</option>
13                                     <option value="53" @(categoryinfoid == 53 ? "selected=selected" : "")>套三</option>
14                                     <option value="54" @(categoryinfoid == 54 ? "selected=selected" : "")>套四</option>
15                                 </select>

此時html標籤裏面屬性與值的對應要求不是很高,可是在.net core 2.2中,html標籤裏面屬性與值的必需要對應要求,上面得這種狀況會報錯mvc

The tag helper 'option' must not have C# in the element's attribute declaration area.

標記幫助器"選項"在元素的屬性聲明區域中不得包含 C# 語句。編輯器

編輯器會認爲,@(「...」)不是一個屬性。spa

解決辦法很奇特,看代碼:.net

 1 <select class="form-control" id="categoryinfoid" name="categoryinfoid">
 2                                     <option value="0">選擇</option>
 3                                     <!option value="22" @(categoryinfoid==22 ? "selected=selected" : "" )>內雙</!option>
 4                                     <!option value="23" @(categoryinfoid==23 ? "selected=selected" : "" )>內三</!option>
 5                                     <!option value="24" @(categoryinfoid==24 ? "selected=selected" : "" )>內四</!option>
 6                                     <!option value="32" @(categoryinfoid==32 ? "selected=selected" : "" )>海雙</!option>
 7                                     <!option value="33" @(categoryinfoid==33 ? "selected=selected" : "" )>海三</!option>
 8                                     <!option value="34" @(categoryinfoid==34 ? "selected=selected" : "" )>海四</!option>
 9                                     <!option value="42" @(categoryinfoid==42 ? "selected=selected" : "" )>陽雙</!option>
10                                     <!option value="43" @(categoryinfoid==43 ? "selected=selected" : "" )>陽三</!option>
11                                     <!option value="44" @(categoryinfoid==44 ? "selected=selected" : "" )>陽四</!option>
12                                     <!option value="52" @(categoryinfoid==52 ? "selected=selected" : "" )>套雙</!option>
13                                     <!option value="53" @(categoryinfoid==53 ? "selected=selected" : "" )>套三</!option>
14                                     <!option value="54" @(categoryinfoid==54 ? "selected=selected" : "" )>套四</!option>
15                                 </select>

每一個要使用@(「...」)的標籤前面加一個「!」號就OK了,不明覺厲code

相關文章
相關標籤/搜索