Я использую if else в представлении Razor, чтобы проверить наличие нулевого значения, например:
@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;
}
и вызвать на View, как:
<td class="shoptableamount">
@Html.Action("GetItemAmount", "Shop", new { fid = item.ShopListFoodID })
</td>