使用数据表服务器端时在表列内呈现局部视图

问题描述

我正在使用实体框架从表中加载项目列表并将以下模型绑定到 Razor 视图

public class ListStudentsviewmodel
{
    [BindNever]
    public List<AppName.Entities.Student> Students { get; set; }

    [DataType(DataType.Date)]
    [display(Name = "Start Date")]
    public DateTime? StartDate { get; set; }

    [DataType(DataType.Date)]
    [display(Name = "End Date")]
    public DateTime? EndDate { get; set; }
}

表定义如下[某些列在运行时通过prtial视图呈现,即_TrackingLink,另外在某些列中,使用相关实体值Exams等]

<table id="shipments" class="table table-hover table-striped table-bordered text-center table-middle">
    <thead>
        <tr>
            <td>OrderNo</td>
            <td>InvoiceUrl</td>
            <td>ExamSummary</td>
            <td>CreatorUsername</td>                 
        </tr>
    </thead>
    <tbody>      
    </tbody>
</table>

目前tbody实现如下

            @foreach (var student in Model.Students)
            {                   
                <tr>
                    <td><a asp-action="GetLabels" asp-route-orderNo="@student.OrderNo" target="_blank">@student.OrderNo</a></td>                 
                    <td><partial name="_TrackingLink" model="student" /></td>
                    <td>
                        @if (@student.Exams != null)
                        {                                
                            //Rendering a column as ExamSummary using few properties of Exam object 
                            @student.Exams.Remarks
                        }
                        else
                        {
                            <span>&nbsp;</span>
                        }
                    </td>
                    <td>@student.CreatorUsername</td>                                           
                </tr>   
            }
            

现在我将数据表转换为服务器端处理,如果我选择数据表的服务器端处理,我如何使用部分视图_TrackingLink 呈现列

我发现渲染选项如下所示,但是我不会得到局部视图的好处,并且这个局部视图引用模型的多列来创建链接

                  $(document).ready(function () {
                    $('#shipments').DataTable({
                        proccessing: true,serverSide: true,ajax: {
                            url: "/Shipments/LoadShipments",type: 'POST',contentType: "application/json",dataType: "json",data: function (d) {
                                
                                return JSON.stringify(d);
                            }
                        },// Columns Setups OrderNo CourierShipmentId NavShipmentNo
                        "columns": [
                            { data: "orderNo"},{
                                data: "InvoiceUrl",render: function (data,type) {
                                    return '<span class="flag"> build content using custom html here </span>'+data;
                                }
                            },{ data: "ExamSummary"},{ data: "CreatorUsername"}
                        ]
                          
                    });
                });
            

还建议将具有所有属性和关系的实体表示传递给视图[在这种情况下,有 12-15 个属性与 Student 模型相关联,但仅使用 4-5 个属性显示在表中 还有很多相关的属性,如 Exams Fees Payments 等,表中只使用了 Exam and Fees 中的几个属性。 ] 那么我们应该为必要的属性创建一个自定义的视图模型还是建议按原样传递整个对象

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)