首页 > 解决方案 > ViewBag 中保存的列表是在 cshtml 视图中打印数据

问题描述

ViewBag.FY我从控制器中保存了一个列表。在调试期间,它会在 ViewBag 中显示数据。

当我在视图中打印数据时,它不是在打印数据。

这是代码:

@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>

请帮助我 - 谁知道这个问题的答案?

提前致谢

标签: asp.netasp.net-corerazorview

解决方案


这是一个工作演示:

模型:

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; }

    }

行动:

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();
        }

看法:

@{
    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>

结果: 在此处输入图像描述


推荐阅读