Foreach in a Viewbag is not working properly

Asked

Viewed 236 times

1

Inside that foreach

@foreach (var item in Model)
        {
            <tr>
                <td>
                    <h3 id="subscription-@(item.SubscriptionId)" data-id="@item.SubscriptionId" class="item-accordion result-header">
                        @item.OrderId
                    </h3>
                </td>
                @if (Html.CurrentCustomer().Type == Atma.SND.CSP.SharedContracts.Contracts.Customers.CustomerTypeRequest.Reseller)
                {
                    <td>
                        @item.CustomerEmail
                    </td>
                }

                @foreach(var dominio in ViewBag.Dominio)
                {
                    <td>@dominio.Domain</td>
                }

                <td>
                    <span class="@(item.Status == "Ativa" ? "status-ativo" : "status-preto")">@item.Status</span>
                    <div id="subscription-content-@(item.SubscriptionId)" class="conteudo-item-accordion result-area display-none"></div>
                </td>
                <td>
                    @foreach (var product in item.Products)
                    {
                        <div>
                            @product
                        </div>
                    }
                </td>
                <td>
                    @Html.ActionLink("Detalhes", "Detail", "Subscription", new { id = item.SubscriptionId }, null)
                </td>
                <td>
                    <a class="see-order-details" href="@Url.Action("GetOrderDetail", new {id = item.OrderId})">Ver pedido</a>
                </td>
            </tr>
        }
        </tbody>

I did it

@foreach(var dominio in ViewBag.Dominio)
{
    <td>@dominio.Domain</td>
}

It turns out that only the first item is printed on the screen and this should be because of being inside a foreach. I fill my Viewbag like this:

List<MicrosoftCustomer> lista = new List<MicrosoftCustomer>();
foreach (var item in customers)
{                    
  lista.AddRange(ListaMicrosoftCustomer(item.CustomerMicrosoftId));
}
ViewBag.Dominio = lista;

Here in Controller are being filled in correctly, but my logic is wrong at the time of clicking on View. How do I correctly print the Domain in my Viewbag?

My model, the same from View

public class SearchSubscriptionResponse
    {
        public int SubscriptionId { get; set; }
        public int OrderId { get; set; }
        public int ResellerId { get; set; }
        public int CustomerId { get; set; }
        public Guid CustomerGuid { get; set; }
        public string CustomerEmail { get; set; }

        public string MicrosoftCustomerId { get; set; }

        public DateTime CreatedOn { get; set; }
        public DateTime RenewOn { get; set; }
        public string Status { get; set; }
        public decimal EstimatedMonthlyPriceBRL { get; set; }
        public bool PodeAtivar { get; set; }
        public bool PodeAlterar { get; set; }
        public List<string> Products { get; set; }
    }

Here I take the Microsoftcustmer

private List<MicrosoftCustomer> ListaMicrosoftCustomer(string microsoftCustomerId)
{
    return _microsoftCustomerService.Table.Where(x => x.Id == microsoftCustomerId).ToList();
}
  • Try to change that line lista.AddRange(ListaMicrosoftCustomer(item.CustomerMicrosoftId)); for lista.Add(item.CustomerMicrosoftId);. I could be wrong, because I don’t understand where you’re from, or what you are ListaMicrosoftCustomer

  • @Matheus, is a method I created to store Microsoftcustomer. I will edit and post it. I edited and posted it

  • I think you want to use AddRange, the method Add, would be enough

  • @Matheus, I think the whole question is not there, but the way the information is extracted in the View. That’s my bottleneck

1 answer

1

You didn’t specify the type of ViewBag.

Change your foreach so that it stays as below:

@foreach(var dominio in ViewBag.Dominio as List<MicrosoftCustomer>)
{
    <td>@dominio.Domain</td>
}

Also check, in this snippet, if it returns more than one value:

return _microsoftCustomerService.Table.Where(x => x.Id == microsoftCustomerId).ToList();

To simplify when checking the records change it so it looks like this:

private List<MicrosoftCustomer> ListaMicrosoftCustomer(string microsoftCustomerId)
{
    var result = _microsoftCustomerService.Table.Where(x => x.Id == microsoftCustomerId).ToList();
    return result;
}

Put a breakpoint in the first line of the method and/or place a Watch in the var resultand check the return of the consultation.

  • It does not return a value just a value, it returns a list of values the way I did. Microsoftcustomerid in this context is not a PK, but I think the problem is that Viewbag is inside a foreach as well, I think that might be it, but I’m changing the approach. I created another property inside the screen Model and I will bring filled, I think that would be better

  • @pnet, edited the answer, his viewbag was not doing the cast, he is a Dynamic and in the foreach he did not recognize as an array to be able to go through.

Browser other questions tagged

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