0

I am trying to get an Application User by its Id but it is always returning null. I need it to chow its details on the edit view but because it is null, my edit view has all fields empty.

This is my controller:

public ActionResult Edit(string id) {

  ApplicationDbContext db = new ApplicationDbContext();
  ApplicationUser user = db.Users.Find(id);
  return View(user);
}

[HttpPost]
public ActionResult Edit(ApplicationUser user) {
    if (ModelState.IsValid) {
      ApplicationDbContext db = new ApplicationDbContext();
      db.Entry(user).State = EntityState.Modified;
      db.SaveChanges();
      return RedirectToAction("Index");

      return View(user);
    }

The problem is that the "db.Users.Find(id);" is returning null. Instead of that If I do the following code everything works:

public ActionResult Edit(string id) {

  ApplicationDbContext db = new ApplicationDbContext();

  ApplicationUser user = new ApplicationUser();
  user.Email = "test@gmail.com";
  //(...)

  return View(user);
}

Can you give me an idea of what can be wrong with this?

Thanks in advance for any help.

Update: Thanks for all your comments. As some of you pointed out the reason is actually very simple - I am searching for id's that don't exist on the DB. However I don't know how it is happening. When I show my ids on a table (check code below), the ids are different from the ones I can see on my table on SQL Object Explorer. How can that happen? Every other information (name and email) is right.

@model IEnumerable<FMS.Models.ApplicationUser>
<table class="table table-striped table-hover ">
  <thead>
    <tr>
      <th>Name</th>
      <th>E-mail</th>
      <th>ID</th>
    </tr>
  </thead>
  <tbody>

    @foreach (var user in Model) {
    <tr>
      <td>@user.Name</td>
      <td>@user.Email</td>
      <td>@user.Id</td>
      <td class="pull-right">
        @Html.ActionLink("Edit", "Edit", new { id = user.Id})
      </td>
    </tr>

    }

  </tbody>
</table>

Update 2: I just noticed that every time I refresh the view, the id's on the ID column are different! What can be causing this?

newhouse-pt
  • 141
  • 2
  • 13
  • see this https://stackoverflow.com/questions/20925822/asp-net-mvc-5-identity-how-to-get-current-applicationuser – kpollock Jul 05 '17 at 10:37
  • Comment is not helping, OP does not need the current user – Ivan Mladenov Jul 05 '17 at 10:38
  • 2
    You have user with that id in the database? What is the code of Find method? Is Id a primary column in the table? Are you pointing to the right database? – Chetan Jul 05 '17 at 10:39
  • 3
    "db.Users.Find(id)" returns null. I would suggest that either your ID field is not the primary key, or that maybe the primary key is an int and you're passing it a string. Or that the ID simply doesn't exist in your database. – ADyson Jul 05 '17 at 10:40
  • ah sorry missed that it was not the current user, many apologies – kpollock Jul 05 '17 at 10:57
  • @ChetanRanpariya Thanks for your reply. Actually I just noticed that the id passed does not exist on the DB but it's very strange. Please check the update above. – newhouse-pt Jul 05 '17 at 12:04
  • @ADyson Thanks for your reply. The ID field is the primary key and is a nvarchar(128). Actually the ID I am searching don't exist on the DB but I don't know why. Please check my update above. – newhouse-pt Jul 05 '17 at 12:06
  • Are you sure you're hitting the same database each time? – mason Jul 05 '17 at 12:08
  • You need to check if you are connecting to the correct database while listing and editing the user info. Did you try the solution suggested in the answers below? – Chetan Jul 05 '17 at 12:09
  • @ChetanRanpariya Thanks for your reply. The database is correct but I just noticed an odd issue. Please check "Update 2" above. Thanks. – newhouse-pt Jul 05 '17 at 12:31
  • What about database? It changes in database too? Do you have entity framework configured for database creation and populating data? – Chetan Jul 05 '17 at 13:17
  • @ChetanRanpariya No, on database the id's do not change.This is very strange. Yes, I have entity framework for that. – newhouse-pt Jul 05 '17 at 13:24
  • the most obvious explanation for the different IDs would be that you're looking at 2 different copies of the database – ADyson Jul 05 '17 at 13:31

2 Answers2

0

You can write following way :

public ActionResult Edit(string id) {

  ApplicationDbContext db = new ApplicationDbContext();
  ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == id);
  return View(user);
}
Dilip Oganiya
  • 1,504
  • 12
  • 20
-2

You can find the user with:

db.Users.FirstOrDefault(u => u.Id.Equals(id))

and if it returns null maybe you do not have such a user

Ivan Mladenov
  • 1,787
  • 12
  • 16