如果/ Else语句在视图中使用剃须刀不起作用

时间:2017-10-05 12:16:48

标签: asp.net-mvc razor

我创建了这段代码并将其传递给了部分视图:

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语句无效,我无法理解原因。任何想法我将如何解决这个问题?

1 个答案:

答案 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>
    }
}