Model value in Razor page is always null

Anjali Agarwal 1,491 Reputation points
2023-01-30T05:05:26.0666667+00:00

I have the following code on my cshtml page:

 @if (Model!=null && Model.EmergencyInfos!=null && Model.EmergencyInfos.ToList().Count>0)
            {
                    var count = Model.EmergencyInfos.Count;
                    @for (var i = 0; i < count; i++)
                    {
                        <div class="form-group row">
                            <div class="col">
                                <label asp-for="@Model.EmergencyInfos.ToList()[i].LastName" class="control-label"></label>
                                <input name="EmergencyInfos[@i].LastName" value="@(string.IsNullOrEmpty(Model.EmergencyInfos.ToList()[i].LastName)? "": @Model.EmergencyInfos.ToList()[i].LastName)" class="form-control" />
                                <span asp-validation-for="@Model.EmergencyInfos.ToList()[i].LastName" class="text-danger"></span>
                           </div>
                            <div class="col">
                            <label asp-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="control-label"></label>
                                <input name="EmergencyInfos[@i].FirstName" value="@(string.IsNullOrEmpty(Model.EmergencyInfos.ToList()[i].FirstName)? "": @Model.EmergencyInfos.ToList()[i].FirstName)" class="form-control" />
                            <span asp-validation-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="text-danger"></span>
                            </div>
}
}

 else
                {
                    @for (var i = 0; i < 2; i++)
                    {

                        <div class="form-group row">
                             <div class="col">
                            <label asp-for="@Model.EmergencyInfos.ToList()[i].LastName" class="control-label"></label>
                                <input name="EmergencyInfos[@i].LastName" class="form-control" />
                                <span asp-validation-for="@Model.EmergencyInfos.ToList()[i].LastName" class="text-danger"></span>
                        </div>
                            <div class="col">
                                <label asp-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="control-label"></label>
                                <input name="EmergencyInfos[@i].FirstName" class="form-control" />
                                <span asp-validation-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="text-danger"></span>
                            </div>
}

I have two rows of value in the database for emergencyInfo, but when I put debugger on this particular line :

(Model!=null && Model.EmergencyInfos!=null && Model.EmergencyInfos.ToList().Count>0)

I always get null for emergencyInfos. Below is the screen shot:


below is the screen shot from the database that shows two rows in the emergencyInfo table:


Not sure why this is happening.
Another issue that I am having is when I first Input the value for LastName, firstName, I get the exception that model state is not valid. I put the debugger on Model state and noticed that, I am forced to input the value for second row of  LastName and firstName, second row is created by the for loop:  
@for (var i = 0; i < 2; i++)
 I just want to put the value for first row of LastName and FirstName. I want the value for second row to be optional, but somehow the model is not allowing that. Below is my model classes:


public partial class EmployeeInfo
{

    
[DisplayName("Last Name")]
    public string LastName { get; set; } = null!;
    [DisplayName("First Name")]
    public string FirstName { get; set; } = null!;
   public virtual ICollection<EmergencyInfo> EmergencyInfos { get; } = new List<EmergencyInfo>();
}

public partial class EmergencyInfo
{
  
 public string LastName { get; set; } = null!;

    public string FirstName { get; set; } = null!;

  public virtual EmployeeInfo EmployeeInfo { get; set; } = null!;
}




ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,674 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,540 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,111 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,236 Reputation points
    2023-06-11T19:49:47.2066667+00:00

    you don't show the code that creates the model, but EmergencyInfos is a navigation property. you need to load the related data. see:

    https://learn.microsoft.com/en-us/ef/core/querying/related-data/

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.