【问题标题】:.net mvc @foreach id for table/cell [duplicate]表/单元格的.net mvc @foreach id [重复]
【发布时间】:2020-10-29 12:11:48
【问题描述】:

加载表格时,我想在单元格 4 的每一行上执行一个 javascript。这将格式化放置在其中的 json 字符串。

我的编码方式只是更新第一行/单元格...

我认为这将归结为正确使用 id 和 getElementById。

        @foreach (var item in Model.list)

        {
            <tr class="table-info">
                <td>@item.library</td>
                <td>@item.api</td>
                <td>@item.ibmiPgm</td>
                <td> <pre id="uglyPretty"></pre> </td>

                <script>

                    var obj = { "success": 1, "resultMessage": "Success", "list": [{ "custNo": "101", "firstName": "First Name: 101", "lastName": "Last Name: 101", "address1": "Address1: 101", "address2": "Address2: 101", "city": "City: 101", "state": "10", "zip": "101", "routing": "101", "accountNo": "101" }, { "custNo": "102", "firstName": "First Name: 102", "lastName": "Last Name: 102", "address1": "Address1: 102", "address2": "Address2: 102", "city": "City: 102", "state": "10", "zip": "102", "routing": "102", "accountNo": "102" }] };
                    document.getElementById("uglyPretty").innerHTML = JSON.stringify(obj, undefined, 2);

                </script>

                <td>
                    <button typeof="button" onclick="location.href='@Url.Action(item.api, "", new { api = item.api, jsonRequest = item.requestExample })'">Consume IBMi Core-iRST API</button>
                </td>
            </tr>
        }

【问题讨论】:

    标签: javascript .net json model-view-controller


    【解决方案1】:

    每个表格行内的脚本部分.. 太棒了.. 无论如何 - 它适用于第一个,因为 javascript 正在通过 Id 查找元素。每行都有相同的 ID,但脚本将更新找到的第一个 ID。如果您有每个元素的唯一 ID,您可以:

        @foreach (var item in Model.list)
        {
            <tr class="table-info">
                <td>@item.library</td>
                <td>@item.api</td>
                <td>@item.ibmiPgm</td>
                <td> <pre id="uglyPretty_@item.Id"></pre> </td>
    
                <script>
    
                    var obj = { "success": 1, "resultMessage": "Success", "list": [{ "custNo": "101", "firstName": "First Name: 101", "lastName": "Last Name: 101", "address1": "Address1: 101", "address2": "Address2: 101", "city": "City: 101", "state": "10", "zip": "101", "routing": "101", "accountNo": "101" }, { "custNo": "102", "firstName": "First Name: 102", "lastName": "Last Name: 102", "address1": "Address1: 102", "address2": "Address2: 102", "city": "City: 102", "state": "10", "zip": "102", "routing": "102", "accountNo": "102" }] };
                    document.getElementById("uglyPretty_@item.Id").innerHTML = JSON.stringify(obj, undefined, 2);
    
                </script>
    
                <td>
                    <button typeof="button" onclick="location.href='@Url.Action(item.api, "", new { api = item.api, jsonRequest = item.requestExample })'">Consume IBMi Core-iRST API</button>
                </td>
            </tr>
        }
    

    【讨论】:

    • 谢谢,但请看我的回答!
    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2014-07-30
    • 2013-02-27
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多