是否可以在ASP.NET MVC中分配HTML按钮ID?

时间:2016-01-26 16:26:40

标签: c# asp.net-mvc html5 razor

我使用for循环来填充表格:

@using (Html.BeginForm(null, null, FormMethod.Get, new { name = "frm", id = "frm" })) {
  <table id="table1" border="1" style ="width:100%">  
    @for (int i = 1; i < 13; i++)
        {
        <tr>
            <td>
                @(i.ToString()+". Details") 
            </td>
            <td>
                <button type="button" onclick="invisiblize(i)">Show detail @i</button>
            </td>
        </tr>
        }
  </table>

是否可以为上述代码后生成的按钮分配ID?

1 个答案:

答案 0 :(得分:2)

我不确定在代码运行后是否需要分配ID的,但是这将允许您在代码运行时设置ID 。 。

@using (Html.BeginForm(null, null, FormMethod.Get, new { name = "frm", id = "frm" })) {
      <table id="table1" border="1" style ="width:100%">  
        @for (int i = 1; i < 13; i++)
            {
            <tr>
                <td>
                    @(i.ToString()+". Details") 
                </td>
                <td>
                    <button id="div@i.ToString()" type="button" onclick="invisiblize(i)">Show detail @i</button>
                </td>
            </tr>
            }
      </table>