asp.net mvc cshtml foreach View nullerror.There is data but it appears not to exist.

Serdar3535 20 Reputation points
2024-10-09T08:56:58.2633333+00:00

asp.net mvc chtml foreach View null error.There is data in foreach line but it appears not to exist.


@{
    ViewBag.Title = "personeller";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<h2>Personeller</h2>
<br />
<link href="~/Content/bootstrap.min.css" rel="stylesheet"/>
<table cless="table table-bordered">
    <tr>
        <th>Personel Ad Soyad</th>
        <th>Kullanıcı Adı</th>
        <th>Tür</th>
        <th>Telefon</th>
        <th>Eposta Adresi</th>
    </tr>
    @foreach (var item in ViewBag.personeller)
    {
      
        <tr>
           
            <td>@item.personeladi</td>
            <td>@item.personelKullaniciadi</td>
            <td>@item.tur</td>
            <td>@item.personeltel</td>
            <td>@item.personeleposta</td>
        </tr>
    }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,512 questions
{count} votes

Accepted answer
  1. SurferOnWww 3,201 Reputation points
    2024-10-10T03:51:35.7966667+00:00

    I suggest:

    (1) Do not use an anonymous type to generate personeller as you did in the controller as follows:

    (x, y) => new  // this will create anonymous type object. Don't do so.
              {
                  personeladi=y.personelAdSoyad,
                  personelKullaniciadi=y.personelKullaniciAd,
                  tur = x.personelTuru,
                  personeltel=y.personelTelno,
                  personeleposta= y.personelEposta
              }
    

    (2) Define custom class like below to transfer the data form controller to view:

    public class DTO
    {
        public string personeladi { get; set; }
        public string personelKullaniciadi { get; set; }
        public string tur { get; set; }
        public string personeltel { get; set; }
        public string personeleposta { get; set; }
    }
    

    (3) Modify the code of (1) above to use the above class (not anonymous type) as follows:

    (x, y) => new DTO  // Create DTO object, not anonymous type object
              {
                  personeladi=y.personelAdSoyad,
                  personelKullaniciadi=y.personelKullaniciAd,
                  tur = x.personelTuru,
                  personeltel=y.personelTelno,
                  personeleposta= y.personelEposta
              }
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Serdar3535 20 Reputation points
    2024-10-09T08:58:08.91+00:00

    1

    2

    0 comments No comments

  2. Lan Huang-MSFT 29,826 Reputation points Microsoft Vendor
    2024-10-09T10:03:16.5433333+00:00

    Hi @Serdar3535,

    Can you provide the code for your controller?

    You need to cast the list to its original type before using it.

    For example:

    @foreach (var item in ViewBag.personeller as List<WebApplication3.Models.Test> )

    You can also change the data returned by the controller as follows:

     private TestContext db = new TestContext();
     public ActionResult Index()
     {
          List<Test> list = db.tests.ToList();
         //list.Add(new Test() { personeladi=1,personeleposta="a",personelKullaniciadi="a",personeltel="a",tur="a"});
         //list.Add(new Test() { personeladi = 2, personeleposta = "b", personelKullaniciadi = "b", personeltel = "b", tur = "b" });           
         ViewBag.personeller = list;
         return View();
     }
    
    <table cless="table table-bordered">
        <tr>
            <th>Personel Ad Soyad</th>
            <th>Kullanıcı Adı</th>
            <th>Tür</th>
            <th>Telefon</th>
            <th>Eposta Adresi</th>
        </tr>
        @foreach (var item in ViewBag.personeller)
        {
            <tr>
                <td>@item.personeladi</td>
                <td>@item.personelKullaniciadi</td>
                <td>@item.tur</td>
                <td>@item.personeltel</td>
                <td>@item.personeleposta</td>
            </tr>
        }
    </table>
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread


  3. Lan Huang-MSFT 29,826 Reputation points Microsoft Vendor
    2024-10-09T10:07:58.86+00:00

    Duplicate post.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.