将ASP.Net窗体标签设置为“模型属性”值

本文关键字:属性 模型 模型属性 Net ASP 窗体 标签 设置 | 更新日期: 2023-09-27 18:25:55

我正在使用一个ASP.NET Forms应用程序,为了简单起见,我添加了模型属性[TotalService]&[PurchasedorReinstatedService],其中我将来自另一个模型的值存储到此属性中,以便在第一个模型中使用。

然后我尝试引用这个Model属性,并为它设置一个ASP Label .Text属性。问题是,我一直得到NullReferenceException,特别是:

An exception of type 'System.NullReferenceException' occurred in PROJECT.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.

这是我得到错误的自定义控件:

using PROJECT.Models.Default;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PROJECT.Classes;
namespace PROJECT.CustomControls.Default
{
    public partial class YearsOfService : System.Web.UI.UserControl
    {
        public List<Years_Of_Service> yos { get; set; }
        public Years_Of_Service_Data yosd { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ApplyData();
            }
        }
        private void ApplyData()
        {
            salaryGridView.DataSource = yos;
            salaryGridView.DataBind();
            decimal totalService = 0;
            foreach (GridViewRow row in salaryGridView.Rows)
            {
                totalService += Convert.ToDecimal(row.Cells[1].Text);
            }
            if (yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString() != null) // NULL REFERENCE HERE
            {
                creditSalLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString();
            }
            else
            {
                creditSalLabel.Text = String.Empty;
            }
        }
    }
}

这是我的Years_Of_Service型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models.Default
{
    public class Years_Of_Service
    {
        public int Year { get; set; }
        public string FiscalYear
        {
            get
            {
                return Year + "-" + (Year + 1);
            }
        }
        public decimal ServiceCredited { get; set; }
        public decimal Salary { get; set; }
        public string CoveredEmployer { get; set; }
        // New properties added for Label Values
        public string TotalService { get; set; }
        public string PurchasedorReinstatedService { get; set; }
    }
}

Years_Of_Service_Data型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models.Default
{
    public class Years_Of_Service_Data
    {
        public List<Years_Of_Service> YearsOfService { get; set; }
    }
}

这是我的Helper中的一段代码,我正在从数据库结果(Oracle Data Reader)读取到我的模型实体中(我已经确认这些值被正确地添加到TotalServicePurchasedorReinstatedService属性中):

            while (odr.Read())
            {
                Years_Of_Service yosRecord = new Years_Of_Service();
                yosRecord.Year = gh.GetSafeInt(odr, 0);
                yosRecord.ServiceCredited = gh.ConvertNullDecimal(odr.GetValue(1));
                yosRecord.Salary = gh.ConvertNullDecimal(odr.GetValue(2));
                yosRecord.CoveredEmployer = gh.GetSafeString(odr, 3);
                // Use the Default Info Model Class - At_A_Glance Model properties to fill these properties for text labels.
                yosRecord.TotalService = defInfoOasis.At_A_Glance.CreditTotal.ToString("N5");
                yosRecord.PurchasedorReinstatedService = defInfoOasis.At_A_Glance.TotalPurchase.ToString("N5");
                yos.Add(yosRecord);
            }

最初我尝试了以下操作,也得到了FirstOrDefault()实体,结果与NullReferenceException:相同

creditSalLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault() != null ? yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString() : String.Empty;
purchaseCreditLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.PurchasedorReinstatedService).FirstOrDefault() != null ? yosd.YearsOfService.OrderByDescending(y => y.PurchasedorReinstatedService).FirstOrDefault().ToString() : string.Empty;

任何人都知道我如何将我的Label.Text属性设置为我的(任何实体)模型属性[TotalService]&[PurchasedorReinstatedService]?按照我设置属性的方式,引用模型类中的每个实体对于这两个属性都将具有相同的值。

将ASP.Net窗体标签设置为“模型属性”值

我的猜测是你正在恢复默认值,即空

yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault()

然后对null执行ToString()。

如果不是这样的话,我会把yosd看作null或yosd。为null的YearsOfService。