Search code examples
javascriptjqueryasp.net-mvcasp.net-core-mvcrazor-pages

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


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

Solution

  • HTTP is stateless, which means that data generated in one request is not persisted. In your case, the branches generated in a GET request are not available in a POST request and need to be regenerated there:

    public async Task OnPost()
    {
        userModel.Branches  = _db.Branch.ToList(); // generate the branches
        if (ModelState.IsValid)
        {
            await _db.User.AddAsync(User);
            await _db.SaveChangesAsync();
            SuccessMessage = "Form submitted successfully!";
         }
    }