C# 是否将ASP.Net表单标签设置为模型属性值?

C# 是否将ASP.Net表单标签设置为模型属性值?,c#,asp.net,null,label,nullreferenceexception,C#,Asp.net,Null,Label,Nullreferenceexception,我正在使用一个ASP.NET表单应用程序,其中(为了简单起见)我添加了模型属性[TotalService]&[PurchaseDorRestoredService],其中我将另一个模型的值存储到该属性中,以便在第一个模型中使用 然后,我尝试引用此模型属性,并为其设置ASP Label.Text属性。问题是,我不断收到NullReferenceExceptions,特别是: An exception of type 'System.NullReferenceException' occurred

我正在使用一个ASP.NET表单应用程序,其中(为了简单起见)我添加了模型属性
[TotalService]
&
[PurchaseDorRestoredService]
,其中我将另一个模型的值存储到该属性中,以便在第一个模型中使用

然后,我尝试引用此模型属性,并为其设置ASP Label
.Text
属性。问题是,我不断收到NullReferenceExceptions,特别是:

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;
            }
        }
    }
}
服务年限\u数据
型号:

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; }
    }
}
最初我尝试了以下操作,也得到了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;

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

我猜您将返回默认值,即null

yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault()
然后在null上执行ToString()


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

看来你是对的,我正在尝试
ToString()
yosd
,就目前情况来看,它是null。这是一个奇怪的交易,我把控件的大部分数据都放在一个网格中,还有两个我正试图引入的单一值(为了简单起见,只需从已经从数据库中获取它们的另一个模型中获取)。
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;
yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault()