You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Ajax获取的MVC控制器JSON数据渲染为HTML表格?

如何将Ajax获取的JSON数据渲染为HTML表格?

没问题!我来帮你搞定这个需求——你已经通过Ajax从MVC控制器拿到了JSON数据,接下来只需要在success回调里动态构建HTML表格,再把它插入到页面的指定容器中就可以了。

第一步:在视图页面添加表格容器

首先,在你的视图里找个合适的位置,添加一个用来承载表格的容器:

<div id="classificationTableContainer"></div>

第二步:修改Ajax回调函数生成表格

接下来,完善你的getClassificators函数,在success回调里处理数据并生成表格HTML:

function getClassificators(){ 
    $.ajax({ 
        url:"/Employee/GetClassificationLevels", 
        type:"GET", 
        success: function(result){ 
            if(Array.isArray(result) && result.length > 0){ 
                // 初始化表格HTML,添加基础样式(你可以根据需求调整)
                let tableHtml = '<table border="1" cellpadding="8" cellspacing="0" style="border-collapse: collapse; width: 100%;">';
                
                // 生成表头
                tableHtml += `
                    <thead>
                        <tr style="background-color: #f2f2f2;">
                            <th>等级</th>
                            <th>名称</th>
                            <th>分数</th>
                            <th>子级信息</th>
                        </tr>
                    </thead>
                `;
                
                // 生成表体内容
                tableHtml += '<tbody>';
                result.forEach(function(classificator){
                    // 处理子级信息:兼容数组或单个对象的情况
                    let subLevelContent = '';
                    if(Array.isArray(classificator.EmpEvaluation_SubLevelView)){
                        // 如果是数组,把所有子级名称用逗号拼接
                        subLevelContent = classificator.EmpEvaluation_SubLevelView.map(item => item.Name).join(', ');
                    } else if(classificator.EmpEvaluation_SubLevelView?.Name){
                        // 如果是单个对象,直接取名称
                        subLevelContent = classificator.EmpEvaluation_SubLevelView.Name;
                    }

                    // 生成当前分类器的表格行
                    tableHtml += `
                        <tr>
                            <td>${classificator.Level}</td>
                            <td>${classificator.Name}</td>
                            <td>${classificator.Score}</td>
                            <td>${subLevelContent || '无'}</td>
                        </tr>
                    `;
                });
                tableHtml += '</tbody></table>';
                
                // 把生成的表格插入到容器中
                $('#classificationTableContainer').html(tableHtml);
            } else {
                // 没有数据时显示提示
                $('#classificationTableContainer').html('<p style="text-align: center; padding: 20px;">暂无分类数据</p>');
            }
        }, 
        error: function(err){ 
            console.log(err); 
            // 请求失败时显示提示
            $('#classificationTableContainer').html('<p style="text-align: center; padding: 20px; color: #dc3545;">加载数据失败,请稍后重试</p>');
        } 
    }); 
}

关键点说明:

  • 我给表格加了基础的样式(比如边框合并、宽度、表头背景色),你可以根据自己的页面风格调整CSS。
  • 处理子级信息时做了兼容:不管返回的EmpEvaluation_SubLevelView是数组还是单个对象,都能正常显示内容,如果没有子级信息会显示“无”。
  • 同时处理了无数据和请求失败的场景,给用户更友好的反馈。

内容的提问来源于stack exchange,提问作者Valery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:18:41