Model is always null on Razor page

Anjali Agarwal 1,366 Reputation points
2023-01-30T05:11:54.52+00:00

hello All,

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 table, but when I put the 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:

User's image

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

User's image

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. 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,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 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.
10,234 questions
{count} votes