List saved in ViewBag is printing data in the cshtml View

Viewed 63

I saved a list in ViewBag.FY from the controller. During debugging it it is showing data in the ViewBag.

When I am printing the data in the view, it is not printing data.

Here is the code:

@model IEnumerable<MVC4_Theming.HRMS_Tax>

@{
    ViewBag.Title = "Index";
    var taxSlabsByFinancialYear = ViewBag.FY;
}

            <div class="table table-responsive">
                <table id="example" class="table table-hover table-striped  table-bordered" cellspacing="0">

                    <thead>
                        <tr>
                            <th>No#</th>
                            <th>Name</th>
                            <th>Min-Range</th>
                            <th>Max-Range</th>
                            <th>Annual-Range</th>
                            <th>Fix-Amount</th>
                            <th>(%)-Amount</th>
                            <th>Year-Slab</th>
                            <th>IsActive</th>
                            <th>Action</th>
                        </tr>

                        @{
                            if (ViewBag.FY == null)
                            {
                                <p>Please Select Financial Year</p>
                            }

                            else
                            {
                                int a = 0;
                                foreach (var item in ViewBag.FY)
                                {
                                    a++;
                                    <tr>
                                        <td>@a</td>
                                        <td>@item.Name</td>
                                        <td>@item.MinAmountRang</td>
                                        <td>@item.MaxAmountRang</td>
                                        <td>@item.Anuual_Amount</td>
                                        <td>@item.Fix_Amount</td>
                                        <td>@item.Percentage_Amount</td>
                                    </tr>
                                }
                            }
                        }
                    </thread>
                </table>
            </div>
        </div>
    </div>
</div>

Please help me - who knows an answer to this problem?

Thanks in advance

2 Answers

Here is a working demo:

Model:

public class FY
    {
        public string Name { get; set; }
        public int MinAmountRang { get; set; }
        public int MaxAmountRang { get; set; }
        public int Anuual_Amount { get; set; }
        public int Fix_Amount { get; set; }
        public int Percentage_Amount { get; set; }

    }

Action:

public IActionResult TestViewBag()
        {
            ViewBag.FY = new List<FY> { 
                new FY {  Name="n1", MaxAmountRang=10, MinAmountRang=1, Fix_Amount=3, Percentage_Amount=4, Anuual_Amount=5} ,
                new FY {  Name="n2", MaxAmountRang=10, MinAmountRang=1, Fix_Amount=3, Percentage_Amount=4, Anuual_Amount=5} ,
                new FY {  Name="n3", MaxAmountRang=10, MinAmountRang=1, Fix_Amount=3, Percentage_Amount=4, Anuual_Amount=5} ,

            };
            return View();
        }

View:

@{
    ViewBag.Title = "Index";
    var taxSlabsByFinancialYear = ViewBag.FY;
}

<div class="table table-responsive">
    <table id="example" class="table table-hover table-striped  table-bordered" cellspacing="0">

        <thead>
            <tr>
                <th>No#</th>
                <th>Name</th>
                <th>Min-Range</th>
                <th>Max-Range</th>
                <th>Annual-Range</th>
                <th>Fix-Amount</th>
                <th>(%)-Amount</th>
                <th>Year-Slab</th>
                <th>IsActive</th>
                <th>Action</th>
            </tr>
    
           
            </thead>
        <tbody>
            @{
                if (ViewBag.FY == null)
                {
                    <p>Please Select Financial Year</p>
                }

                else
                {
                    int a = 0;
                    foreach (var item in ViewBag.FY)
                    {
                        a++;
                        <tr>
                            <td>@a</td>
                            <td>@item.Name</td>
                            <td>@item.MinAmountRang</td>
                            <td>@item.MaxAmountRang</td>
                            <td>@item.Anuual_Amount</td>
                            <td>@item.Fix_Amount</td>
                            <td>@item.Percentage_Amount</td>
                        </tr>
                    }
                }
            }
        </tbody>
    </table>
</div>

result: enter image description here

try this, you should Cast the ViewBag Data0 to a list

@model IEnumerable<MVC4_Theming.HRMS_Tax>

    @{
        ViewBag.Title = "Index";
        var taxSlabsByFinancialYear = (**YourListName**)ViewBag.FY; 
    }
Related