Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何首先使用EF数据库中的存储过程填充DataTable_Asp.net_.net_Asp.net Mvc - Fatal编程技术网

Asp.net 如何首先使用EF数据库中的存储过程填充DataTable

Asp.net 如何首先使用EF数据库中的存储过程填充DataTable,asp.net,.net,asp.net-mvc,Asp.net,.net,Asp.net Mvc,我有一个存储过程GetAllUsers,它返回来自表用户的所有列 下面是我想要实现的DataTable的示例: 我使用实体框架数据库优先的方法 存储过程: ALTER PROCEDURE [dbo].[GetAllUsers] AS BEGIN SELECT * FROM Users END 控制器: public ActionResult Index() { // Create ViewModel/ResourcViewModel insta

我有一个存储过程GetAllUsers,它返回来自表用户的所有列

下面是我想要实现的DataTable的示例:

我使用实体框架数据库优先的方法

存储过程:

ALTER PROCEDURE [dbo].[GetAllUsers]
AS
BEGIN
    SELECT * 
    FROM Users
END
控制器:

    public ActionResult Index()
    {
        // Create ViewModel/ResourcViewModel instance
        var vm = new ResourceViewModel();

        // Code to fetch data from stored procedure and display to DataTable
    }
视图模型:

public class ResourceViewModel
{
    public int UserID { set; get; }
    public string FirstName { set; get; }
    public string LastName { set; get; }
}
视图:


一旦我将用户列表显示到DataTable中。我还想知道如何将UserID绑定到“查看详细信息”按钮。

您的方法似乎有点不合适。使用MVC,理想的方法是将一个模型返回到页面中,该页面包含您所需要的内容

public class IndexViewModel
{
      public List<ResourceViewModel> Resources { get; set; } 
}
那么你的观点应该更像

@model IndexViewModel
<table class="table" id="dataTable">
    <thead>
        <tr>
           <th class="text-center">First Name</th>
           <th class="text-center">Last Name</th>
           <th class="text-center">Actions</th>

       </tr>
   </thead>
   <tbody>
@foreach(var resource in Model.Resources)
{

    <tr>
        <td>@resource.FirstName</td>
        <td>@resource.LastName</td>
        <td><button class="btn btn-xs btn-primary" data-toggle="modal" data-target="#exampleModal">View Details</button>
        </td>
    </tr>

    <tr>
        <td>Black</td>
        <td>Panther</td>
        <td><button class="btn btn-xs btn-primary" data-toggle="modal" data-target="#exampleModal">View Details</button>
        </td>
    </tr>

}
</tbody>
</table>
所以你的控制器动作应该更像

public ActionResult Index()
{
    // Create ViewModel/ResourcViewModel instance
    var vm = new IndexViewModel();

    // Code to fetch data from stored procedure and display to DataTable
    vm.Resources = new List<ResourceViewModel>();

    foreach(var user in GetAllUsers())
    {

        Resources.Add(new ResourceViewModel(){
            FirstName = user .FirstName,
            LastName = user .LastName,
            UserId = user .UserId
        });
    }

    return View(vm);
}
显然,这只是一段伪代码,您需要正确地调用存储过程。不过,存储过程的另一种选择是使用存储库模式,使用查询语法,允许您通过lamda where子句。

参考此-在每行或有条件地实现自定义操作按钮

定义Datatables列布局并根据需要进行自定义

例如:

参考资料:

select*from users不需要存储过程,你不这样认为吗?我计划很快添加一个变量来过滤掉一个特定的用户id。select*from users,其中id=myvariable?!!对我只是想知道如何先从SP获取数据到DataTable。如果你能提供一个建议,那将是巨大的帮助。我似乎无法使.each出现在调用存储过程之后。我有sp_GetAllUsers_结果,我也可以调用sp_GetAllUsers。你知道为什么吗?抱歉,每个都是我框架中的一个扩展。只要使用一个标准的foreach循环,我就会给出答案
public ActionResult Index()
{
    // Create ViewModel/ResourcViewModel instance
    var vm = new IndexViewModel();

    // Code to fetch data from stored procedure and display to DataTable
    vm.Resources = new List<ResourceViewModel>();

    foreach(var user in GetAllUsers())
    {

        Resources.Add(new ResourceViewModel(){
            FirstName = user .FirstName,
            LastName = user .LastName,
            UserId = user .UserId
        });
    }

    return View(vm);
}
$('#example').DataTable( {
        ajax: "../php/staff.php",
        columns: [
            { data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.first_name+' '+data.last_name;
            } },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: "start_date" },
            { data: "salary", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) },
            {
                data: null,
                className: "center",
                defaultContent: '<a href="" class="editor_edit">Edit</a> / <a href="" class="editor_remove">Delete</a>'
            }
        ]
    } );