我创建了这段代码并将其传递给了部分视图:
MealCurrentDetails mcd = new MealCurrentDetails
{
id = 1,
name = "Name",
description = "desc",
price = 99,
availability = 0
};
return PartialView("MealDetails", mcd);
这是我创建的部分视图。
@model WebProg.Models.MealCurrentDetails
@if (Model != null)
{
<dt>Meal Name</dt>
<dd>
@Html.DisplayFor(model => model.name)
</dd>
<dt>Description</dt>
<dd>
@Html.DisplayFor(model => model.description)
</dd>
<dt>Price</dt>
<dd>
@Html.DisplayFor(model => model.price)
</dd>
<dt>Availability</dt>
if(model.availability == 0)
{
<dd class="text-success">Available</dd>
}
else
{
<dd class="text-warning">Not available</dd>
}
}
我的视图中的if语句无效,我无法理解原因。任何想法我将如何解决这个问题?
答案 0 :(得分:1)
应该是Model.availability
而不是model.availability
。
@model WebProg.Models.MealCurrentDetails
@if (Model != null)
{
<dt>Meal Name</dt>
<dd>
@Html.DisplayFor(model => model.name)
</dd>
<dt>Description</dt>
<dd>
@Html.DisplayFor(model => model.description)
</dd>
<dt>Price</dt>
<dd>
@Html.DisplayFor(model => model.price)
</dd>
<dt>Availability</dt>
if(Model.availability == 0)
{
<dd class="text-success">Available</dd>
}
else
{
<dd class="text-warning">Not available</dd>
}
}