如何使用实体框架DF上的存储过程填充DataTable

时间:2022-05-30 21:40:12

I have a stored procedure GetAllUsers which returns all the columns from a the table Users.

我有一个存储过程GetAllUsers,它返回表Users中的所有列。

Here is a sample of a the DataTable I want to achieve.

这是我想要实现的DataTable的示例。

如何使用实体框架DF上的存储过程填充DataTable

I'm using Entity Framework (Database First).

我正在使用实体框架(数据库优先)。

Stored Procedure

ALTER PROCEDURE [dbo].[GetAllUsers]
AS
BEGIN

      SELECT * FROM Users
END

Controller

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

       // Code to fetch data from Stored Procedure and display to DataTable
    }

View Model

public class ResourceViewModel
{
    // Dropdown Properties
    public int UserID { set; get; }
    public string FirstName { set; get; }
    public string LastName { set; get; }
}

View

<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>
    <tr>
        <td>John</td>
        <td>Wick</td>
        <td><button class="btn btn-xs btn-primary" data-toggle="modal" data-target="#exampleModal">iew 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>

Once I have displayed the list of users into the DataTable. I want to bind the UserID to my View Details button.

一旦我将用户列表显示到DataTable中。我想将UserID绑定到我的查看详细信息按钮。

1 个解决方案

#1


0  

1.Calling the stored procedure from EF

1.从EF调用存储过程

public ActionResult Index()
{
    using (DBContext dbContext = new DBContext())
    {
         // Code to fetch data from Stored Procedure
         var vm = dbContext.[YourStoredProcedureName].Select(new ResourceViewModel(){... set values here}).ToList();
         return view(vm);
    }        
}
  1. Binding the UserID

    绑定UserID

    @model IEnumerable<ResourceViewModel>
      <table>
        <tr>           
            //set th
        </tr>    
        @foreach (var item in Model)
        {
            <tr>
                //set td
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id })//here you can change it to ViewDetails action link
                </td>
            </tr>
        }
    </table>
    

#1


0  

1.Calling the stored procedure from EF

1.从EF调用存储过程

public ActionResult Index()
{
    using (DBContext dbContext = new DBContext())
    {
         // Code to fetch data from Stored Procedure
         var vm = dbContext.[YourStoredProcedureName].Select(new ResourceViewModel(){... set values here}).ToList();
         return view(vm);
    }        
}
  1. Binding the UserID

    绑定UserID

    @model IEnumerable<ResourceViewModel>
      <table>
        <tr>           
            //set th
        </tr>    
        @foreach (var item in Model)
        {
            <tr>
                //set td
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id })//here you can change it to ViewDetails action link
                </td>
            </tr>
        }
    </table>