0

I have a button in my Index.html page which shows another view page: Reports.cshtml, there is a table inside the page, now I want to remove this button and let the table showing on my Index.html page directly, but when I paste the table to the code it shows an error: Error

Part of my view code are showed below:

<table id="hardware-data-table" class="table table-striped table-hover">
                            <thead bgcolor="silver">
                                <tr>
                                    <th hidden="hidden">
                                        @Html.LabelFor(model => model.Report_HardwareListByExpiration.FirstOrDefault().InvHardwareID)
                                    </th>
                                    <th>
                                        @Html.LabelFor(model => model.Report_HardwareListByExpiration.FirstOrDefault().Equipment)
                                    </th>
                                    <th>
                                        @Html.LabelFor(model => model.Report_HardwareListByExpiration.FirstOrDefault().HardwareModel)
                                    </th>
                                    <th>
                                        @Html.LabelFor(model => model.Report_HardwareListByExpiration.FirstOrDefault().WL_EndDateFormatted)
                                    </th>
                                </tr>
                            </thead>
                            <tbody>                                   
                                @foreach (var item in Model.Report_HardwareListByExpiration)
                                {
                                    if (item.WL_EndDate < DateTime.Now && item.WL_EndDate > DateTime.Now.AddYears(-99))
                                    {
                                        <tr>
                                            <td hidden="hidden">
                                                @item.InvHardwareID
                                            </td>
                                            <td>
                                                @item.Equipment
                                            </td>
                                            <td>
                                                @item.HardwareModel
                                            </td>
                                            <td style="background-color: #ff726f">@item.WL_EndDateFormatted</td>
                                        </tr>
                                    }
                                    if (item.WL_EndDate > DateTime.Now && item.WL_EndDate < DateTime.Now.AddYears(99))
                                    {
                                        <tr>
                                            <td hidden="hidden">
                                                @item.InvHardwareID
                                            </td>
                                            <td>
                                                @item.Equipment
                                            </td>
                                            <td>
                                                @item.HardwareModel
                                            </td>
                                            <td style="background-color: orange">
                                                @item.WL_EndDateFormatted
                                            </td>
                                        </tr>
                                    }
                                }
                            </tbody>
                        </table>

My Report controller code are showed below:

public class ReportsController : Controller
{
    // GET: Report
    public ActionResult Reports()
    {
        if (Session["UserID"] == null || !(bool)Session["IsLoggedIn"])
        {
            return RedirectToAction("Login", "Account");
        }
        ViewModel myViewModel = new ViewModel
        {
            User = GetSessionInfoFromSessions(),
            Params = new ParametersModel
            {
                Start_Date = new DateTime(2015, 12, 31),
                End_Date   = DateTime.Now.AddDays(60)
            }
        };
        myViewModel.Report_HardwareListByExpiration    = InvHardwareModel.Report_HardwareListByExpiration(myViewModel);
        
        return View(myViewModel);
    }

And my hardware Model:

public static List<InvHardwareModel> Report_HardwareListByExpiration(ViewModel myViewModel)
    {
        try
        {
            var myAssManEnt = new AssetManagementEntities();
            var myUspList = myAssManEnt.usp_Report_InvHardware_ByExpirationDates
                (
                    agencyID  : myViewModel.User.AgencyID,
                    deptID    : myViewModel.User.DeptID,
                    roleID    : myViewModel.User.RoleID,
                    startDate : myViewModel.Params.Start_Date,
                    endDate   : myViewModel.Params.End_Date
                ).ToList();
            var myReturnList = new List<InvHardwareModel>();
            foreach(usp_Report_InvHardware_ByExpirationDates_Result myItem in myUspList)
            {
                myReturnList.Add(Models.InvHardwareModel.ToModel(myItem));
            }
            return myReturnList;
        }
        catch(Exception e)
        {
            throw ErrorHandler.MyException(e, "InvHardwareModel.Report_HardwareListByExpiration");
        }
    }

The code works perfect when its in the other view page, but shows exception when I move it to my home page, any ideas? Thank you so much!

Acadia
  • 1
  • 3
  • Check the error message `myViewModel.Report_HardwareListByExpiration` return null, So it will show this error in `@foreach{}`, You need to debug `Report_HardwareListByExpiration` method to check where cause the error . – Xinran Shen Jan 11 '23 at 08:47
  • Thank you Xinran, now I know the problem is I need to find a way to passing data from one to another, thank you! – Acadia Jan 11 '23 at 17:56

0 Answers0