我使用的是如果別人在Razor視圖來檢查空值是這樣的:如果在剃刀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>
}
但是,不管我的模型量爲空或有一個值時,所呈現的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>
您將不得不向我們展示模型本身的外觀,特別是Model.Amount字段。也許嘗試@if(string.IsNullOrEmpty(item.Amount))而不是? – 2011-12-18 02:42:31
嗨,感謝您的幫助,但金額是一個小數,如果我按照這裏說的一個,它沒有工作。無論如何,請參閱我的編輯,因爲我決定在控制器中做到這一點,謝謝:) – shennyL 2011-12-18 02:56:51
如果其他組合確實無法在剃刀中使用。 – Anderson 2014-12-10 15:47:41