简体   繁体   中英

Getting error while trying to display a list in view in ASP.NET Core MVC

I'm getting an error

Object reference not set to an instance and object

when trying to run this ASP.NET Core MVC app.

I'm trying to display the list of all the properties that I'm getting from the controller.

I'm getting the error at this line:

@foreach(var s in Model)

Can someone please help me in figuring out what am I doing wrong?

Index.cshtml :

@Using System.Collections
@Using ESBReference
@model IEnumerable<DebitCardListingResponseDTOCardInfo>;

@{
    ViewData["Title"] = "Home page";
}

@foreach(var s in Model)
{
  <li>@s.ACCOUNTNUMBER</li>
  <li>@s.CARDNAME</li>
  <li>@s.CARDNUMBER</li>
  <li>@s.CARDSTATUS</li>
  <li>@s.CARDEXPIRYDATE</li>
}

HomeController :

public class HomeControler : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
       _logger = logger;
    }

    public async Task<IActionResult> IndexAsync()
    {
        try
        {
            DebitCardServiceClient cli = new DebitCardServiceClient();
            DebitCardList list = new DebitCardList();
            DebitCArdListingRequestDTO reqDTO = new DebitCArdListingRequestDTO();
            reqDTO.cnic = "35***********";

            RequestHeader requestheader = new RequestHeader();
            requestheader.SystemName = "SSK";
            requestheader.RequestID = "" + DateTime.Now.TOFileTime();
            requestheader.TenantID = "***************";
 
            list.RequestHeader =  requestheader;
            list.RequestHeader =  reqDTO;

            var resp = await cli.DebitCardListAsync(list);

            foreach (var card in resp.DebitCardListResponse.DebitCardListResponse1.Cards)
            {
                var s = card.ACCOUNTNUMBER.ToString();
                s += card.CARDNAME.ToString();
                s += card.CARDNUMBER.ToString();
                s += card.CARDSTATUS.ToString();
                s += card.CARDEXPIRYDATE.ToString();
            }

            ViewData["card"] = resp.DebitCardListResponse.DebitCardListResponse1.Cards;
        }
        catch(exception ex)
        {
            Console.WriteLine(ex);
        }

        return View();
    }
}

In your Index.cshtml , you use @model IEnumerable<DebitCardListingResponseDTOCardInfo>; , so in your controller, you need to pass the IEnumerable<DebitCardListingResponseDTOCardInfo> to the view.

I write a simple demo here:

Model:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

Controller:

public IActionResult Privacy()
{
    // For testing convenience, I just hard code here.
    List<Student> students = new List<Student>()
            {
                new Student()
                {
                    Id = 1,
                    Name = "Jack",
                    Country = "USA"
                },
                new Student()
                {
                    Id = 2,
                    Name = "Nancy",
                    Country = "UK"
                },
                new Student()
                {
                    Id = 3,
                    Name = "Lily",
                    Country = "Cn"
                }
            };

    // I just return view() here!!!
    return View();
}

View:

@model List<Student>

@foreach(var s in Model)
{
  <li>@s.Id</li>
  <li>@s.Name</li>
  <li>@s.Country</li>
}

You can see there is an NullReferenceException in @foreach()

在此处输入图像描述

But if I write code like this in controller:

public IActionResult Privacy()
{
    // For testing convenience, I just hard code here.
    List<Student> students = new List<Student>()
                {
                    new Student()
                    {
                        Id = 1,
                        Name = "Jack",
                        Country = "USA"
                    },
                    new Student()
                    {
                        Id = 2,
                        Name = "Nancy",
                        Country = "UK"
                    },
                    new Student()
                    {
                        Id = 3,
                        Name = "Lily",
                        Country = "Cn"
                    }
                };

    // I return List<Student> from controller to view
    return View(students );
}

It will work fine.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM