C#mvc datatables不希望每个var项都包含在任何解决方案中?

C#mvc datatables不希望每个var项都包含在任何解决方案中?,c#,asp.net-mvc,foreach,datatables,C#,Asp.net Mvc,Foreach,Datatables,我试图使用datatables显示详细信息表,但出现以下错误。 为此,我的ViewModel如下所示 public class EquipmentDetailsViewModel { public int Equipment_Id { get; set; } public IEnumerable<EquipmentServiceHistoryListViewModel> Service_History { get; set; } }

我试图使用datatables显示详细信息表,但出现以下错误。

为此,我的ViewModel如下所示

     public class EquipmentDetailsViewModel
    {
        public int Equipment_Id { get; set; }
        public IEnumerable<EquipmentServiceHistoryListViewModel> Service_History { get; set; }
}
公共类设备详细信息查看模型
{
公共int设备_Id{get;set;}
公共IEnumerable服务_历史记录{get;set;}
}
我的控制器如下所示:

    public ActionResult JsonDetails(int Id)
    {
        Equipment e = _repo.GetSingle(Id);

        var model = AutoMapper.Mapper.Map<Equipment, EquipmentDetailsViewModel>(e);

        JsonResult jsonModel = new JsonResult()
        {
            Data = model,
            ContentEncoding = System.Text.Encoding.UTF8,
            ContentType = "application/json; charset=utf-8",
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            MaxJsonLength = Int32.MaxValue
        };

        return jsonModel;

    }
    public ActionResult Details(int Id)
    {
        return View();
    }
public ActionResult jsondeails(int-Id)
{
设备e=_repo.GetSingle(Id);
var模型=AutoMapper.Mapper.Map(e);
JsonResult jsonModel=新的JsonResult()
{
数据=模型,
ContentEncoding=System.Text.Encoding.UTF8,
ContentType=“application/json;charset=utf-8”,
JsonRequestBehavior=JsonRequestBehavior.AllowGet,
MaxJsonLength=Int32.MaxValue
};
返回jsonModel;
}
公共行动结果详细信息(int Id)
{
返回视图();
}
在该控制器的视图中,我使用下表:

@using Project.ViewModels;
@model EquipmentDetailsViewModel
<table id="dataTable" class="datatables table table-striped table-hover ">
    <thead>
        <tr>
            <th>
                Service No
            </th>
            <th>
                Service Due Date
            </th>
            <th>
                Service Completed Date
            </th>
        </tr>
    </thead>
    <tbody>
@使用Project.ViewModels;
@模型设备详细信息查看模型
服务编号
服务到期日
服务完成日期

@foreach(模型服务历史记录中的变量项)
{
@Html.DisplayFor(model=>item.Service\u No)
@Html.DisplayFor(model=>item.Sevice\u Due\u Date)
@Html.DisplayFor(model=>item.Service\u Completed\u Date)
}
我的JS设置如下

    @section scripts{

    <script>
        $(document).ready(function () {
            $('.datatables').DataTable({
                "lengthMenu": [[25, 50, -1], [25, 50, "All"]],
                 "ajax": {
                    "url": '/Service_History/JsonDetails/',
                    "dataSrc": ""
                },
                "deferRender": true,
                "columns": [
                    {
                        data: 'Service_No',
                        visible: false
                    },


                    { data: 'Service_Due_Date',
                        render: function (data, type, row) {                           
                            if (type === 'display' || type === 'filter') {

                                return (moment(data).format("DD/MM/YYYY"));
                            }                           
                            return data;
                        }
                    },

                    {
                        data: 'Service_Completed_Date',
                        render: function (data, type, row) {
                            if (type === 'display' || type === 'filter') {
                                return (moment(data).format("DD/MM/YYYY"));
                            }                        
                            return data;
                        }
                    }                    
                ],
                "order": [] //prevent auto sorting on first column
            });
});


    </script>

}
@节脚本{
$(文档).ready(函数(){
$('.datatables').DataTable({
“长度菜单”:[[25,50,-1],[25,50,“全部”],
“ajax”:{
“url”:“/Service_History/jsondeails/”,
“dataSrc”:”
},
“延迟渲染”:正确,
“栏目”:[
{
数据:“服务号”,
可见:假
},
{数据:'服务到期日',
呈现:函数(数据、类型、行){
如果(类型=='display'| |类型=='filter'){
返回(时刻(数据).format(“DD/MM/YYYY”);
}                           
返回数据;
}
},
{
数据:“服务完成日期”,
呈现:函数(数据、类型、行){
如果(类型=='display'| |类型=='filter'){
返回(时刻(数据).format(“DD/MM/YYYY”);
}                        
返回数据;
}
}                    
],
“顺序”:[]//防止在第一列上自动排序
});
});
}
有人能告诉我项目中每个var的替代方案吗 我可以用它来做这个页面吗。
谢谢

您似乎正在尝试从客户端获取结果,而不是从服务器加载任何数据

因此,不需要foreach循环。对JsonResult函数的datatables调用应该足够了,但是查看代码时,您没有将ID参数传递给javascript中的端点

当尝试使用razor时,请记住处理是在服务器端完成的,因此

需要

    public ActionResult Details(int Id)
    {
        return View();
    }
成为

public ActionResult Details(int Id)
{
   var viewmodel = _repo.GetSingle(Id);
    return View(viewmodel );
}

我还想在foreach循环之前添加一个非空的服务历史检查,如果您试图从客户端获取结果,并且不希望从服务器加载任何数据

因此,不需要foreach循环。对JsonResult函数的datatables调用应该足够了,但是查看代码时,您没有将ID参数传递给javascript中的端点

当尝试使用razor时,请记住处理是在服务器端完成的,因此

需要

    public ActionResult Details(int Id)
    {
        return View();
    }
成为

public ActionResult Details(int Id)
{
   var viewmodel = _repo.GetSingle(Id);
    return View(viewmodel );
}

我还将在foreach循环之前添加一个非空的服务历史检查,这不是错误消息。它只是对一行代码的引用。你是在什么背景下看到的?实际的问题是什么?您返回的是一个json结果,而视图看起来是分开的。我所看到的视图中没有提供模型,因此从未实例化服务历史。把这作为一个评论,因为我不认为我了解全部情况。您可以详细说明或显示视图模型的设置位置吗?
public ActionResult Details(int-Id){return view();}
不返回任何模型数据。这是应该声明页面加载时使用的模型的位置。foreach Razor语法由服务器根据您(应该)在此方法中提供的模型数据进行解释。我假设JsonDetails方法仅用于某些ajax调用,因此将在其他时间调用它。设置一些断点并查看代码中实际执行的内容。实际上,在看到您的编辑。。。如果在创建表时立即通过ajax加载数据,那么您可能根本不需要Razor
foreach
。如果用户在加载后只需等待一两分钟,数据将通过ajax获取,对吗?感谢您的响应。我使用foreach循环,因为在我的viewmodel中,我有一个服务历史viewmodel的IEnumerable列表。如果在视图中不使用foreach循环