【问题标题】:ASP.NET Core MVC button visibilityASP.NET Core MVC 按钮可见性
【发布时间】:2020-12-26 11:35:14
【问题描述】:

隐藏我之前添加到表格单元格的多个按钮。是否可以使用单个按钮控件顺序使其可见?如何用 c# 做到这一点?

网络上有布局、html、css 等教育内容,但我找不到与我想做的相关的任何内容。我怎么解决这个问题?

【问题讨论】:

  • 抱歉,您的意思不太清楚。您是否在问如何显示每个按钮,一个接一个,例如按钮 1 出现,然后按钮 2 出现,然后按钮 3 出现,等等?
  • 这看起来像是一个与 javascript 相关的问题。与 c# 或 asp.net 无关
  • 我想隐藏我添加到表格中的按钮。我想用我在表格外添加的另一个按钮使按钮可见。 javascript可以做到这一点,但我想知道它是否可以在c#中完成。
  • @MuratKorkmaz c# 是一种服务器端语言,它可以帮助您生成 HTML 内容(使用某种复杂的模板,如 Razor View 或 Razor Page)。它无法控制在客户端(浏览器)上呈现的 HTML 按钮。如果您想要一些 c# 代码的魔力来执行某些客户端任务(替换 javascript),也许可以尝试查看所谓的 Blazor 应用程序(.net 核心支持的新功能)。但后来它不再是asp.net core mvc了。
  • 谢谢,您的回答对我很有帮助。

标签: c# html css asp.net-mvc asp.net-core


【解决方案1】:

如果你想用c#来做,当点击添加按钮时,你需要传递你想要传递的数据来传递你想要显示的数量。这是一个演示:

查看(TestTable.cshtml):

<table>
    <thead>
        <tr>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Amount > 0)
        {
            var count = 0;
            @for (var i = 0; i < Model.Amount / 5 + 1; i++)
            {
                <tr>
                    @for (var j = 0; j < 5; j++)
                    {
                        count++;
                        if (count <=Model.Amount&&count<=Model.Buttons.Count)
                        {
                            <th><button>@Model.Buttons[5 * i + j]</button></th>
                        }
                        else
                        {
                            break;
                        }

                    }
                </tr>
            }
        }

    </tbody>
</table>
<form method="post">
    <input hidden name="Amount" value="@Model.Amount">
    <input hidden name="Add" value="1">
    <button>Add</button>
</form>

行动:

public IActionResult TestTable(TableModel t)
        {
            List<string> l = new List<string> { "b0", "b1", "b2", "b3", "b4", "b5" };
            
            TableModel table = new TableModel { Buttons = l ,Amount=0,Add=0};
            if (t.Add == 1 && t.Amount >= 0) {
                table.Amount = ++t.Amount;
            }
                return View(table);
        }

型号:

public class TableModel {
        public List<string> Buttons { get; set; }
        public int Amount { get; set; }
        public int Add { get; set; }
    }

结果:

【讨论】:

  • 是的,这正是我想知道的,我很好奇。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
相关资源
最近更新 更多