我在Razor视图中使用if else来检查这样的空值:
@foreach (var item in Model) { <tr id="@(item.ShopListID)"> <td class="shoptablename">@Html.displayFor(modelItem => item.Name) </td> <td class="shoptableamount"> @if (item.Amount == null) { Html.display("--"); } else { String.Format("{0:0.##}",item.Amount); } </td> </tr> }
但是,无论我的模型数量是null还是具有值,所呈现的html不包含任何值。
我不知道为什么会发生这种情况。任何想法?
谢谢…
编辑:
决定在控制器中做到:
// Function to return shop list food item amount public string GetItemAmount(int fid) { string output = ""; // Select the item based on shoplistfoodid var shopListFood = dbEntities.SHOPLISTFOODs.Single(s => s.ShopListFoodID == fid); if (shopListFood.Amount == null) { output = "--"; } else { output = String.Format("{0:0.##}",shopListFood.Amount); } return output; }
并在视图中调用如下:
<td class="shoptableamount"> @Html.Action("GetItemAmount","Shop",new { fid = item.ShopListFoodID }) </td>
解决方法
你必须使用@()
@if (item.Amount == null) { @("--"); } else { @String.Format("{0:0.##}",item.Amount) }
如注释和其他答案所述,Html.display不用于显示字符串,而是用于显示ViewData字典或模型中的数据。阅读http://msdn.microsoft.com/en-us/library/ee310174%28v=VS.98%29.aspx#Y0
原文地址:https://www.jb51.cc/aspnet/253023.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。