15 Kasım 2015

Asp.Net MVC ve Entity Framework İle Veritabanı İşlemleri (Veri Listeleme)

Merhaba,

Önceki yazımda anlattıklarımdan sonra bu yazıda EF ile veri listeleme işlemini yapacağız. Önceki projemiz üzerinden devam edelim.

İlk olarak CustomerController'a Customer listeleyecek bir Action yazalım ve parantezleri içine sağ tıklayıp Add View diyelim:

    [HttpGet]
     public ActionResult List()
     {
         return View();
     }


Oluşturduğumuz bu View bizim için List.cshtml içinde aşağıdaki kodu üretti:

@model IEnumerable<BlogEFApp.Models.Customers>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>List</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Insert")
    </p>
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CompanyName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContactName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContactTitle)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.City)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Region)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PostalCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Country)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Phone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Fax)
            </th>
            <th></th>
        </tr>
   
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Region)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PostalCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Phone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fax)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }
   
    </table>
</body>
</html>


Buradaki @Html.ActionLink("Create New""Create")'da Create yazan yere önceki yazımda Customers tablosuna kayıt atmak için oluşturduğumuz Action'ın adı olan Insert yazarsak Create New yazısına tıkladığımızda yeni kayıt yaptığımız Insert View'ı açılacaktır. Küçük bir not olarak paylaşmak istedim.

Daha önce oluşturduğumuz NWBusinessLogic.cs içine de Customers tablosunu listeleyen (kritersiz) bir metod yazalım:

        public List<Customers> ListCustomer()
        {
            using (var ctx = new NORTHWNDEntities())
            {
                return ctx.Customers.ToList();
            }
        }

Şimdi bu metodu hiç vakit kaybetmeden CustomerController'daki List Action'ında kullanalım:

       [HttpGet]
        public ActionResult List()
        {
            NWBusinessLogic nwBusiness = new NWBusinessLogic();
            return View(nwBusiness.ListCustomer());
        }

Daha önce de belirttiğim gibi sizler ayrı bir yerde metodlarınızı yazmak yerine direkt controller'ın içinde de aynı işlemi yaptırabilirsiniz. Ben yukarıdakini tercih ediyorum.

Build edip çalıştıralım ve browser'a şu linki yazalım: /Customer/List. Customers tablosundaki verilerin şu şekilde listelendiğini göreceksiniz:


Sonraki yazımda da bu listeleme üzerinden kayıt seçip düzenleme (edit) işlemini anlatacağım.

Kolay gelsin.



Hiç yorum yok:

Yorum Gönder