如何在mvc视图中的视图条件下隐藏/显示按钮?

时间:2012-04-20 10:16:58

标签: asp.net-mvc asp.net-mvc-3 model-view-controller

我希望在hide/showbutton condition,但无法在视图内进行此操作。

@{
    ViewBag.Title = "Mapping";
    WebGrid grid = null;
    if (ViewBag.Mappings != null)
    {
        grid = new WebGrid(source: ViewBag.Mappings,
                                defaultSort: "Id",
                                canPage: true,
                                canSort: true,
                                rowsPerPage: 10);
    }
}
<h3>@ViewData["Message"]</h3>
@if (grid != null)
{
    @grid.GetHtml(
                tableStyle: "grid",
                headerStyle: "head",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
    grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Index", new { uid = (Guid)item.id, userAction = "Edit" }, new { @class = "edit" })
    @Html.ActionLink("Delete", "Index", new { uid = (Guid)item.id, userAction = "Delete" }, new { @class = "Delete" })</text>),
                                            grid.Column("PricingSecurityID"),
                                            grid.Column("CUSIP"),
                                            grid.Column("Calculation")
                                          )

                )
}

if(@ViewBag.Mappings != null)
  {    
@Html.ActionLink("Update", "Index", new { userAction = "Update" }, new { @class = "Update" })
}

在上面的视图中,如果ViewBag.Mappings != null那么我正在填充webgrid。 如果填充了webgrid,我需要在webgrid下显示一个Update按钮,但是我在哪里出错才能达到这个条件?

1 个答案:

答案 0 :(得分:2)

从底部第4行移动“@”:

在:

if(@ViewBag.Mappings != null)

后:

@if(ViewBag.Mappings != null)