Error Object reference not set to an instance of an object. on Model.userModel.Branches after button submit click and insert data success?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-05-07T07:38:10.52+00:00

I working on MVC razor pages asp.net core 7 . I can't display success message after submit and insert data on same page .

I get error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

error happen on Model.userModel.Branches

Expected Result is display success message after insert data success but after click submit button

data inserted and give me this error null reference exception

so How to solve this error ?

public class AddUserModel : PageModel
    {
        [BindProperty]
        public AddUserViewModel userModel { get; set; }
        [BindProperty]
        public UC.ADC.Core.Entities.SQL.User  User { get; set; }
        public string SuccessMessage { get; set; }
public AddUserModel(ADCContext db,OracleContext ora_db2)
        {
            
             _db = db;
            userModel = new AddUserViewModel();
        }
     public async Task<IActionResult> OnPost()
            {
                if (ModelState.IsValid)
                {
                    await _db.User.AddAsync(User);
                    await _db.SaveChangesAsync();
                    SuccessMessage = "Form submitted successfully!";
                    return Page();
                    
    
                }
                
            }
    public void OnGet()
            {
                userModel.Branches  = _db.Branch.ToList();
             }
  }

on adduser.cshtml

@page "/AddUser"
<form method="post">
       

        <div class="form-group row">
            <label for="branch-select" class="col-sm-1 col-form-label" style="font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Branch</label>
            <div class="col-sm-3">
                <select id="branch-select" asp-for="User.iBranchCode" class="form-control" style=" margin-left:10px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
                    <option value="">-- Select Branch --</option>
                 
                     @foreach (var branch in Model.userModel.Branches)
                    {
                        <option value="@branch.iBranchCode">@branch.vBranchDesc</option>
                    }
                </select>
            </div>
        </div>
<div class="form-group row" style="margin-left:200px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
            <button type="submit" class="col-sm-1 btn btn-primary">Submit</button>
           
        </div>
</form

so Data insert success after submit button and after that error display and message success not display

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,293 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,374 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,766 Reputation points
    2023-05-07T13:40:24.6133333+00:00

    The OnPost() method does not populate the userModel like the OnGet() does.

    public async Task<IActionResult> OnPost()
    {
        if (ModelState.IsValid)
        {
            await _db.User.AddAsync(User);
            await _db.SaveChangesAsync();
            SuccessMessage = "Form submitted successfully!";
            userModel.Branches  = _db.Branch.ToList();
            return Page();
        }  
    }
    
    0 comments No comments