Assign a Viewbag from an @HTML.Action request

476 Views Asked by At

I am firing a DevExpress Gridivew when a page is requested

@Html.Action("ActionName") 

Inside this Actionesult method, the code populates the gridview and I would like to use a viewbag to show the total number of items inside the grid.

// we have the row count - so assign it. 
ViewBag.NoOpenItems = List<People>.Count;

return PartialView("GridView", List<People>);

So once the grid is populated and the viewbag assigned, the viewbag doesn't show anything inside it.

<h5>Total @ViewBag.NoOpenItems</h5> 

However, when I assign a value to the viewbag inside the main GET Action method on page load, the viewbag value is shown correctly on the page, so I am guessing its to do with assigning the viewbag inside the action method? - Actually as I write this, I am guessing the problem lies with the action method returning a PartialView?

if so, does anyone know how I can achieve what I like to do?

2

There are 2 best solutions below

0
On

you can use Gridivew Pager property to show no of record

settings.SettingsPager.Position = PagerPosition.TopAndBottom;
    settings.SettingsPager.FirstPageButton.Visible = true;
    settings.SettingsPager.LastPageButton.Visible = true;
    settings.SettingsPager.PageSizeItemSettings.Visible = true;
    settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };

for more you can visit Devexpress GridView page

1
On

You can directly use it in View if Model is List<T> instead of putting in ViewBag:

@model List<People>

@if(Model != null)
{
<h5>Total @Model.Count</h5>
}

or:

<h5> @(Model != null ? Model.Count : 0)</h5>