Как заполнить DataTable с помощью хранимой процедуры на Entity Framework DF - PullRequest
0 голосов
/ 08 мая 2018

У меня есть хранимая процедура GetAllUsers, которая возвращает все столбцы из таблицы Users.

Вот пример таблицы данных, которую я хочу получить.

enter image description here

Я использую Entity Framework (База данных сначала).

Хранимая процедура

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
{
    // 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>

Как только я отобразил список пользователей в DataTable. Я хочу связать UserID с моей View Details кнопкой.

1 Ответ

0 голосов
/ 09 мая 2018

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. Привязка идентификатора пользователя

    @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>
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...