List error is null but I am filling

Asked

Viewed 49 times

0

I have this Action in my controller

[HttpGet]
        public ActionResult Details(AzureDiscountGroupModel modelD)
        {
            var discount = _azureDiscountGroupService.GetAll();
            var list = new ResellerListModel();

            var resellers = _resellerService.QueryAll()
                .Include(r => r.WhiteLabels)
                .ToList();

            foreach(var item in resellers)
            {
                list.Id = item.Id;
                list.Name = item.Name;
                list.Alias = item.Alias;
                list.MpnId = item.ResellerMpnId;
                //list.CreatedOn = item.AcceptContractDate;
                //list.WhiteLabel = item.WhiteLabels
            }

            ViewBag.Desconto = discount.Where(x => x.Id > 0);

            ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id);

            return View(modelD);
        }

I’m filling out this table like this

@foreach (var item in ViewBag.DetailReseller as IEnumerable<ResellerListModel>)
    {
    <tr align="center">
        <td>
            @item.Name
        </td>
        <td>
            @item.Alias
        </td>
        <td>
            @item.WhiteLabel
        </td>
        <td>
            @item.MpnId
        </td>
        <td>
            @item.CreatedOn
        </td>
    </tr>
    }

The moment I try to fill in, I get this mistake

System.Nullreferenceexception: 'Object reference not set to an instance of an object.'

How do I iterate through this model?

EDIT1

I made that change, to see if I can

[HttpGet]
        public ActionResult Details(AzureDiscountGroupModel modelD)
        {
            var discount = _azureDiscountGroupService.GetAll();
            var list = new List<ResellerListModel>();

            var resellers = _resellerService.QueryAll()
                .Include(r => r.WhiteLabels)
                .ToList();

            foreach(var item in resellers)
            {
                list.Add(CreateListModelFrom(item));
            }

            ViewBag.Desconto = discount.Where(x => x.Id > 0);

            ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id);

            return View(modelD);
        }

and this one

private ResellerListModel CreateListModelFrom(Reseller reseller)
        {
            var model = new ResellerListModel();

            model.Id = reseller.Id;
            model.Name = reseller.Name;
            model.Alias = reseller.Alias;
            model.MpnId = reseller.ResellerMpnId;

            model.WhiteLabel = true;

            var firstWhiteLabel = reseller.WhiteLabels.OrderBy(x => x.CreatedOn).FirstOrDefault();
            model.CreatedOn = firstWhiteLabel.CreatedOn.ToLocalTime();

            return model;
        }

And yet I can’t iterate through the list in cshtml

If I do so, the list is filled in, but I miss the items that should be shown in the table

@foreach (var item in ViewBag.DetailReseller as IEnumerable<List<ResellerListModel>>)

But if I do so, it is null error

@foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
  • Apparently correct. Try to materialize the list using a . Tolist() when you arrow the data in ViewBag.DetailReseller = resellers.Where(x => x.AzureDiscountGroupId == modelD.Id); and make sure the data is filled.by debugging this line.

  • Putting a break, I saw that the Viewbag is null due to this here: ViewBag.DetailReseller = list.Where(x => x.AzureDiscountGroupId == modelD.Id);. I’m not populating this field in the model: AzureDiscountGroupId

  • Probably the popular field the query will return data.

  • So, the Azurediscountgroupid field is fixed at 2 and I don’t know why this and even setting the model.id == 2 still gives error. The lambda is not working and need to bring the screen ID to the method

  • You missed a Tolist() in the Viewbag lambda, but I need to understand why the fixed field is coming in 2

1 answer

1

Doing so resolved

[HttpGet]
        public ActionResult Details(AzureDiscountGroupModel modelD)
        {
            var discount = _azureDiscountGroupService.GetAll();
            var list = new List<ResellerListModel>();

            var resellers = _resellerService.QueryAll()
                .Include(r => r.WhiteLabels)
                .ToList();

            foreach(var item in resellers)
            {
                list.Add(CreateListModelFrom(item));
            }

            ViewBag.Desconto = discount.Where(x => x.Id > 0);

            ViewBag.DetailReseller = list.Where(x => x.AzureDiscountGroupId == modelD.Id).ToList();

            return View(modelD);
        }

And at View I did

@foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
    {
    <tr align="center">
        <td>
            @(item.Name)
        </td>
        <td>
            @(item.Alias)
        </td>
        <td>
            @(item.WhiteLabel ? "Sim" : "Não")
        </td>
        <td>
            @(item.MpnId)
        </td>
        <td>
            @(item.CreatedOn)
        </td>
    </tr>
    }

The method CreateListModelFrom has as popular purpose the Resellerlistmodel

  • What you’ve changed and realized?

  • 1

    @Cypherpotato, the ToList() in ViewBag, the change to load the ViewBag, the creation of this new method CreateListModelFrom, the List<>in View and I don’t know what else, rs. Those were the changes.

  • What is this CreateListModelFrom makes? It would be interesting to put in the answer, since it can help other people with situations similar to yours.

  • 1

    Okay, but she’s already in the main post and not to stretch the answer too much, I’ll just talk about what it does

  • Oh yes, I hadn’t noticed. Forgive me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.