.net mvc Razor 三元运算判断赋值 ?:

时间:2023-03-09 17:07:44
.net mvc Razor 三元运算判断赋值 ?:

预期效果:

当Model中的Age数字列值为0时,不显示该值。

<input type="text" class="form-control onlyPositiveNumber" id="Age" 
maxlength="3" style="text-align:right;width:80px; " value='@(Model.Age == 0? "":Model.Age.ToString())' >

预期效果:

根据ViewData中的值,判断radio是否需要被选中

<input type="radio" id="rdoReportTypeBS" name="ReportType" disabled="disabled" value="BS"
  @(ViewData["ReportType"].ToString().ToUpper() == "BS"?"checked":"") >