微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在使用实体框架从表中加载项目列表并将以下模型绑定到 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 举报,一经查实,本站将立刻删除。