ASP.NET Core MVC 按钮可见性

问题描述

隐藏我之前添加到表格单元格的多个按钮。是否可以使用单个按钮控件依次使其可见?我怎样才能用 c# 做到这一点?

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

enter image description here

解决方法

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

视图(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; }
    }

结果: enter image description here