Compare the number of rows of a list in a Viewbag

Asked

Viewed 19 times

0

I need to know how many lines(in view) I send from my controller in a type list<> for a viewbag in the view. With this enable or not the option to create new images.

Controller:

    ViewBag.Imagens =             _supplierAdvertImageRepository.GetSupplierAdvertImage(obj.SupplierAdvertID,
                obj.SupplierID);

View

@{
    if (ViewBag.Imagens > 5)
    {
        <div id="div_inseret">
            @Html.ActionLink(
                "Inserir Imagem ", 
                "CreateAdvertImage",
                "SupplierAdvert", 
                new {
                    supplierId = Model.SupplierID, 
                    SupplierAdvertID = Model.SupplierAdvertID}, 
                    new {
                        supplierId = Model.SupplierID, 
                        SupplierAdvertID = Model.SupplierAdvertID, 
                        @class = "btn btn-info"
                    }
            )
        </div>
    }
    else
    {
        <label for="Adress">Limite de Imagens Atingido
            <span class="required">*</span>
        </label>
    }

but it is not correct, someone has a better idea?

1 answer

0


Do it like this:

var lista = _supplierAdvertImageRepository
    .GetSupplierAdvertImage(obj.SupplierAdvertID, obj.SupplierID);
ViewBag.Imagens = lista;
ViewBag.Quantidade = lista.Count();

In the View

@if (int.TryParse(ViewBag.Quantidade.ToString(), out int c) && c > 5)
{
}
  • 1

    Virgil, I hadn’t thought of this much simpler solution, but resolved thank you!

Browser other questions tagged

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