-1

I have 2 Models.

public class LS : BaseDomainModel
{
    [Required]
    public string LsCompanyName { get; set; }

}

and

public class LSName : BaseDomainModel
{
    [Required]
    public string SurveyorName { get; set; }

    [Required]
    public int LSId { get; set; }
    public virtual LS LS { get; set; }

}

In my razor page, I try to view the data:

@if (LsnModel == null) 
{
  <div class="alert alert-info">Loading list..</div>
} 
else 
{
  @foreach (var Item in LsnModel)
   {
     <div>@Item.SurveyorName   @Item.LS.LsCompanyName  </br></div>
   }
}

@code
{

private List<LSName> LsnModel;

protected async override Task OnInitializedAsync()
{
    LsnModel = await _client.GetFromJsonAsync<List<LSName>>("api/lsnames");
}
}

If I remove @Item.LS.LsCompanyName from the razor code, it runs fine. Otherwise, this error came out..

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at WebApp.Client.Pages.LSName.Index.BuildRenderTree(RenderTreeBuilder __builder)
at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, 
RenderFragment renderFragment) at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch
(RenderQueueEntry renderQueueEntry) at Microsoft.AspNetCore.Components.RenderTree.Renderer
.ProcessRenderQueue()

Both Models have data in it with LSName.LSId = LS.Id

BaseDomainModel:

    public abstract class BaseDomainModel
{
    [Key]
    public int Id { get; set; }

    public string CreatedBy { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public Nullable<DateTime> CreatedDate { get; set; }

    public string UpdatedBy { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public Nullable<DateTime> UpdatedDate { get; set; }
}

Where should I check on what is causing this?

Waller
  • 433
  • 1
  • 6
  • 20
  • `Item.LS` is null. Are you sure you are pulling all the related records as well ? – Jawad May 24 '21 at 15:16
  • 1
    Use `@Item.LS.LsCompanyName??"No Company Name"`, in this way you can see all those whose values are null for `LsCompanyName` – Hassan Monjezi May 24 '21 at 15:22
  • `@Item.LS?.LsCompanyName ?? ""` will stop the crash. Your LS property is `null`. – Brian Parker May 24 '21 at 15:34
  • I found out the problem Its in the controller. I didnt declare the LS model in my LSName Get method. var includes = new List { "LS" }; var lsname = await _unitOfWork.LSNames.GetAll(includes: includes); . Thanks guys. – Waller May 25 '21 at 02:47

1 Answers1

0

Make sure you set up the LS item as a new instance. You will then have an empty string rather than a null one. @Hassan also gives a nice solution