【问题标题】:how to hide/show button on view condition inside mvc view?如何在 mvc 视图内的视图条件下隐藏/显示按钮?
【发布时间】:2012-04-20 10:16:58
【问题描述】:

我想在conditionhide/showbutton,但不能在视图内进行。

@{
    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 按钮,但是在条件下我哪里出错了?

【问题讨论】:

  • 会发生什么?链接是否仍然生成?
  • 你在控制器中设置 ViewBag.Mappings 吗?

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


【解决方案1】:

将第 4 行的“@”移到底部:

之前:

if(@ViewBag.Mappings != null)

之后:

@if(ViewBag.Mappings != null)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2021-02-12
    • 2023-03-28
    相关资源
    最近更新 更多