Home > Software engineering >  How can I fetch list with getbyname
How can I fetch list with getbyname

Time:12-14

I have people with the same name in my table. I want the information of people with the same name as getby name to come.

 private static List<Personel> _personeller = new List<Personel> {
   new Personel{Id=1,Ad="Cagatay",Soyad="Kızıltan",TcKimlikNo="11111111",DogumTarihi= new DateTime(1993,1,22) },
   new Personel{Id=2,Ad="Ali",Soyad="Kızıltan",TcKimlikNo="11111112",DogumTarihi=new DateTime(1992,1,22) },
   new Personel{Id=3,Ad="Ali",Soyad="Kızıl",TcKimlikNo="11111113",DogumTarihi= new DateTime(1991,1,22)},
   new Personel{Id=4,Ad="Ahmet",Soyad="Kızıltan",TcKimlikNo="11111113",DogumTarihi= new DateTime(1990,1,22)}
};
public ActionResult PersonelAra()
  {
  return View();
  }
 @{
  ViewBag.Title = "PersonelAra";
  }

<h2>Personel Ara</h2>

 @using (Html.BeginForm())
  {
  <input type="text" name="personelAdi" />
  <br />
  <input type="submit" value="Ara" />
  }

public class Personel
 {
      public int Id { get; set; }

      public string Ad { get; set; }

      public string Soyad { get; set; }

      public string TcKimlikNo { get; set; }

      public DateTime DogumTarihi { get; set; }
 
   }
[HttpGet]
  public ActionResult PersonelAramaSonucu(string personelAdi)
  {

   var aramaSonucundakiPersoneller = _personeller.Where(p => p.Ad.Contains(personelAdi)).ToList();

   return View(aramaSonucundakiPersoneller);

 }

Currently, when I enter the name of Ali, only the first person information is displayed.

{Id=2,Ad="Ali",Soyad="Kızıltan",TcKimlikNo="11111112",DogumTarihi=new DateTime(1992,1,22) },

what i want should be like this:

{Id=2,Ad="Ali",Soyad="Kızıltan",TcKimlikNo="11111112",DogumTarihi=new DateTime(1992,1,22) },
{Id=3,Ad="Ali",Soyad="Kızıl",TcKimlikNo="11111113",DogumTarihi= new DateTime(1991,1,22)},

CodePudding user response:

Do you want to try this way ? Below is a work demo, hope it can help you.

In Controller:

        [HttpGet]
        public ActionResult PersonelAramaSonucu(string personelAdi)
        {

            var aramaSonucundakiPersoneller = _personeller.Where(p => p.Ad.Contains(personelAdi)).ToList();

            return Json(aramaSonucundakiPersoneller);

        }

In PersonelAra.cshtml:

@{
    ViewBag.Title = "PersonelAra";
}

<h2>Personel Ara</h2>

@using (Html.BeginForm("PersonelAramaSonucu", "Getbyname", FormMethod.Get))
{
    <input type="text" name="personelAdi" />
    <br />
    <input type="submit" value="Ara" />
}

result:

enter image description here

OR

use return View(aramaSonucundakiPersoneller); in your PersonelAramaSonucu method.

In the PersonelAramaSonucu.cshtml:

@model IEnumerable<Personel>

@foreach (var item in Model)
{
    <strong>Id: </strong> @Html.DisplayFor(modelItem => item.Id)
    <br />
    <strong>Ad: </strong> @Html.DisplayFor(modelItem => item.Ad)
    <br />
    <strong>Soyad : </strong> @Html.DisplayFor(modelItem => item.Soyad)
    <br />
    <strong>TcKimlikNo : </strong> @Html.DisplayFor(modelItem => item.TcKimlikNo)
    <br />
    <strong>DogumTarihi : </strong> @Html.DisplayFor(modelItem => item.DogumTarihi)
    <br />
}

result:

enter image description here

  • Related