1

I'm trying to add a controller for the ApplicationUser.cs so that when a admin is logged in, they have the ability to view, edit, delete any records from the dbo.AspNetUsers table however I think I'm doing it wrong. This run with Error "NullReferenceException: Object reference not set to an instance of an object."Any idea?

AdminController:

public AdminController (ApplicationDbContext application)
{
    _application = application;

        [HttpGet]
        public IActionResult ActiveUser() { return View();}


        [HttpPost]
        public async Task<ActionResult> ActiveUser(ApplicationUser Model)
        {
            var active = await _application.Users.FindAsync(Model.Email);

            Model.IsActive = true;
            active.IsActive = (Model.IsActive==true);

         await _application.SaveChangesAsync(); 

            return View();
        }

View : ` @model ApplicationUser

            <input asp-for="IsActive" class="form-control" />
            <span asp-validation-for="IsActive" class="text-danger"></span>
        </div>
        <button type="submit" class="btn btn-success">ok</button>`
Mohadese99
  • 51
  • 7

4 Answers4

2

You are actually never setting the IsActive on the user object. Note there is a difference between '=' and '=='.

Try this code:

 ApplicationUser active = await _application.Users.FindAsync(Model.Email);
 active.IsActive = true;
 _application.Update(active);
 await _application.SaveChangesAsync();
shobhit vaish
  • 951
  • 8
  • 22
  • I did as you said " [HttpGet] public IActionResult ActiveUser() { return View();} [HttpPost] public async Task ActiveUser(ApplicationUser Model) { ApplicationUser active = await _application.Users.FindAsync(Model.Email); active.IsActive = Model.IsActive; _application.Update(active); await _application.SaveChangesAsync(); return View(); } " – Mohadese99 Apr 27 '20 at 09:49
  • Are you actually setting the IsActive to true in the view ? – shobhit vaish Apr 27 '20 at 09:50
  • No.but now i change it and compiler can't accepted " active.IsActive = (active.IsActive == true); " iv View – Mohadese99 Apr 27 '20 at 10:22
  • I have changed my answer. Please try the updated code. – shobhit vaish Apr 27 '20 at 10:27
  • should I make new table and put these information there? how to set IsActive in view? – Mohadese99 Apr 27 '20 at 10:40
  • Sorry I am not sure what do you mean? – shobhit vaish Apr 28 '20 at 06:31
2

The error message indicates you are trying to access properties from a null reference. Based on the code provided, there are two probable causes:

  1. The "await _application.Users.FindAsync(Model.Email);" is not finding the user and returning null, hence, when you try to do "active.IsActive = (Model.IsActive==true);" you get the exception.

  2. The "Model" variable is not getting an instance and you are getting a null reference exception. You need to verify you are sending the payload in the "POST" and, also, decorate the "Model" parameter with "[FromBody]" attribute. See below:

[HttpPost]
public async Task<ActionResult> ActiveUser([FromBody]ApplicationUser Model)
{
  // Always check the parameters....
  if (Model == null)
  {
     return View();
  }

  var active = await _application.Users.FindAsync(Model.Email);

  // Always verify if found...
  if (active == null)
  {
    Response.StatusCode=404;
    return View(); 
  }

  active.IsActive = (Model.IsActive==true);

  await _application.SaveChangesAsync(); 

  return View();
}
Luis
  • 866
  • 4
  • 7
  • When submitting the post... Are you able to get to "ActiveUser" function?...or...Do you get the "ERROR 415" on submitting the post?... It appears to be problem related to the "Content-Type" header missing or with the wrong type. Also, try removing the "[FromBody]" attribute IF you are sending the data in the URL. – Luis Apr 29 '20 at 18:33
1

You need to assign value for Model.IsActive:

if (active.IsActive == false) 
{ 
    Model.IsActive = (active.IsActive == true); 
}
_application.Update(active);
await _application.SaveChangesAsync(); 
return View();
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

Your code seems correct. But it seems that you need to pass model object to View. In your case it seems that your View will need object active to be passed.

Try return View(active); instead of return View().

Karan
  • 12,059
  • 3
  • 24
  • 40