Remzi Balakisiyev
Remzi Balakisiyev

Reputation: 53

Why is AppUser null?

Hello guys I have a problem with my asp.net core project. So I built the Register form and I retrieve data of the user from the Register view but When I click a submit button Nothing happened and I seeing appUser is null Please Help meenter image description here

public IActionResult Register()
        {
            return View(new UserSignUpViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> Register(UserSignUpViewModel model)
        {
            if (ModelState.IsValid)
            {
                AppUsers appUsers = new AppUsers();

                appUsers.Name = model.Name;
                appUsers.UserName = model.UserName;
                appUsers.SurName = model.SurName;
                
               
                var result = await userManager.CreateAsync(appUsers, model.Password);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return View(model);
        }

Upvotes: 0

Views: 317

Answers (2)

bashis
bashis

Reputation: 1243

The attached screenshot demonstrates the state prior to the assignment. For this reason the appUsers.Name is not set yet (and hence is null). The appUsers object is not null, otherwise you would've had a NullReferenceException on this line. Just jump into the next line and see appUsers.Name populated with model.Name value.

Upvotes: 2

Abrar Ahmad
Abrar Ahmad

Reputation: 100

your code looks fine but you need to check your Model binding in view.

Upvotes: 1

Related Questions