在原来的.net framework mvc中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标签里面属性与值的必须要对应要求,上面得这种情况会报错
The tag helper 'option' must not have C# in the element's attribute declaration area.
标记帮助器"选项"在元素的属性声明区域中不得包含 C# 语句。
编辑器会认为,@(“...”)不是一个属性。
解决办法很奇特,看代码:
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了,不明觉厉