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

huangapple go评论68阅读模式
英文:

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 .

我正在使用MVC Razor页面开发ASP.NET Core 7,但无法在提交后在同一页面上显示成功消息并插入数据。

I get error

我收到以下错误消息:

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

System.NullReferenceException:'对象引用未设置为对象的实例。'

error happen on Model.userModel.Branches

错误发生在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 ?

那么,如何解决这个错误?

英文:

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

答案1

得分: 1

HTTP是无状态的,这意味着在一个请求中生成的数据不会被保留。在您的情况下,通过GET请求生成的分支在POST请求中不可用,需要在那里重新生成:

public async Task OnPost()
{
    userModel.Branches = _db.Branch.ToList(); // 生成分支
    if (ModelState.IsValid)
    {
        await _db.User.AddAsync(User);
        await _db.SaveChangesAsync();
        SuccessMessage = "表单提交成功!";
    }
}
英文:

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!";
     }
}

huangapple
  • 本文由 发表于 2023年5月7日 15:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定