Here is the Idea. When an Admin is logged on they can pull up a list of all of the users.It will give the options for edit, details, delete like normal but I have added a link to Purchases like so:

```
    @model IEnumerable<IdentitySample.Models.ApplicationUser>

    @{
ViewBag.Title = "Index";
    }
      <div class="col-12 backgroundImg">
        <div class="navbarSpace">
    <div class="col-12 formBackground">
       <h2 class="formHeader">List of Users</h2>

        <h4 class="formText">
            @Html.ActionLink("Create New ", "Create")
        </h4>
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Email)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.UserName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.FavStrain)
                </th>
                <th>

                </th>
            </tr>

            @foreach (var item in Model)
        {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Email)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.UserName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.FavStrain)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id 
         }) |
                        @Html.ActionLink("Details", "Details", new { id = 
          item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = 
        item.Id }) |
                        @Html.ActionLink("Purchases", "PurchaseIndex", new { 
        id = item.Id})

                    </td>
                </tr>
            }
             </table>
          </div>
        </div>
             </div>enter code here

```

When you click the Purchases link it takes you to the PurchaseIndex page which looks like this:
[Purchase List](https://i.stack.imgur.com/1bVS3.png)

```
       @model IEnumerable<IdentitySample.Models.Purchases>

        @{
ViewBag.Title = "Index";
      }
      <div class="col-12 backgroundImg navbarSpace">
        <div class="col-12 formBackground">
    <h2 class="formHeader">Index</h2>
    <hr />
    <div class="formHeaderSmall">
        Total Points <br />
        @Model.Sum(i => i.Points) </div>
    <p class="formText">
        @Html.ActionLink("Create New", "CreatePurchase")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Points)
            </th>

            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Points)
                </td>

                <td></td>
            </tr>
        }
     </table>
    <p class="formText">
        @Html.ActionLink("Back to List", "Index")
    </p>
     </div>
     </div>

```

It gives a list of Purchases and gives the total points that is why i didnt include a details page. Everything works right EXCEPT for the fact that the Purchases do not map to a specific user. If I create a new user and click Purchases it brings up a list of all of the purchases, not just the purchases specific for that user. How do I get a Purchase to map to a Specific User?

I have created a Purchases class that looks like this:

```
  public class Purchases
{
    [Key]
    public int PurchaseId { get; set; }

    [Required]
    [Display(Name = "Product Name")]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [Required]
   [Range(0,5)]
    [Display(Name = "Points")]
    [DataType(DataType.Text)]
    public int Points { get; set; }

    public string ApplicationUserId { get; set; }
    public virtual ApplicationUser Users { get; set; }
}

```

My ApplicationUser Class looks like this:

```
 public class ApplicationUser : IdentityUser
{
    [Display(Name ="Favorite Strain")]
    [DataType(DataType.Text)]
    public string FavStrain { get; set; }
    public virtual List<Purchases> Purchase { get; set; }

```

Now up to this point the Database is registering the Foreign Key of the Purchases Class to the ApplicationUser class like it is supposed to.

I can create a new purchase and display them to a list and all of the Crud Operations work just fine.
The problem is when I create a new Purchase it doesn't include the ApplicationUserId in the Database it returns a Null.

[Null Database](https://i.stack.imgur.com/1geUb.png)

I am pretty sure that the problem is in my Controller. I have tried just about everything so I don't want to include the failed try's so here is the Controllers as they are now and working.

There is no need for me to include the edit or details because I am not going to give the users that access.

```
 public ActionResult CreatePurchase()
    {

        return View();
    }
    private ApplicationDbContext db = new ApplicationDbContext();

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreatePurchase([Bind(Include = "PurchaseId,Name,Points,Id")] Purchases purchases)
    {
        if (ModelState.IsValid)
        {
            db.Purchases.Add(purchases);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(purchases);
    }

    // GET: Purchases/Edit/5
    public ActionResult PurchaseIndex()
    {

        var userDetails = db.Purchases.Include(u => u.Users);
        return View(db.Purchases.ToList());
    }

```

This is my first Question on Stack Overflow so forgive me if something isn't right.

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Update\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

This is my PurchaseIndexController. Now this returns only the user associated with the purchase. However it is always 0 because there is no UserID. If I try using an int? type or Guid? it gives an error. Cannot implicitly convert type int to string.

```
           public ActionResult PurchaseIndex(string ID)
    {
        //this gets all purchases for a certain individual
        ApplicationDbContext db = new ApplicationDbContext();
        var userDetails = db.Purchases.Where(x => x.ApplicationUserId == 
   ID).ToList();
        return View(userDetails);

    }

```

Here is the CreatePurchase View

```
    @model IdentitySample.Models.Purchases

@{
ViewBag.Title = "Create";
}
 <div class="col-12 backgroundImg navbarSpace">
<div class="col-12 formBackground">
    <h2 class="formHeader">Add a New Purchase</h2>
    <hr />
    @using (Html.BeginForm())
    {

        @Html.AntiForgeryToken()
      @*@Html.Hidden("id", (string)ViewBag.UserID)*@
        @Html.HiddenFor(model => model.ApplicationUserId)
        <div class="form-horizontal">

            <div class="col-12">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { 
          @class = "formText col-12" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { 
        htmlAttributes = new { @class = "col-12" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new 
      { @class = "text-danger" })
                </div>
            </div>

            <div class="col-12">
                @Html.LabelFor(model => model.Points, htmlAttributes: new { 
      @class = "formText col-12" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Points, new { 
      htmlAttributes = new { @class = "col-12" } })
                    @Html.ValidationMessageFor(model => model.Points, "", 
      new { @class = "text-danger" })
                </div>
            </div>

            <div class="col-12">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-
           default" />
                </div>
            </div>
        </div>
    }

    <div class="formText">
        @Html.ActionLink("Back to List", "Index")
    </div>
</div>
      </div>

```

I also have link in the Manage section for the users to check thier points and purchases but I don't know how to create an ActionLink for this to just get purchases associated with the user.
 Here is the controller

```
   public ActionResult WeedPoints(string ID)
    {
        ApplicationDbContext db = new ApplicationDbContext();
        var userDetails = db.Purchases.Where(x => x.ApplicationUserId == 
    ID).ToList();
        return View(userDetails);

      }

```

Here is the Action Link now.

```
<div class="col-12 formHeaderSmall">@Html.ActionLink("My 
 Purchases/Points", "WeedPoints", "Manage")</div>

```

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Update\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Here is the Controllers with the View Bag reference. The Create Purchase View has the ViewBag I just Uncommented it out.

```
 [Authorize(Roles = 
"Admin,DispensaryManager,DispensaryEmployee,DispensaryEastEmployee")]
    public ActionResult CreatePurchase(string Id)
    {
        ViewBag.UserID = Id;

        //ApplicationDbContext db = new ApplicationDbContext();
  //var userDetails = db.Purchases.Where(x => x.ApplicationUserId == Id;

        return View();

    }
    private ApplicationDbContext db = new ApplicationDbContext();
    //POST: Purchases/Create

    [HttpPost]
    [Authorize(Roles = 
      "Admin,DispensaryManager,DispensaryEmployee,DispensaryEastEmployee")]
    [ValidateAntiForgeryToken]
    public ActionResult CreatePurchase([Bind(Include = 
      "PurchaseId,Name,Points,ApplicationUserId")] Purchases 
    purchases,string id)
    {
        if (ModelState.IsValid)
        {

            db.Purchases.Add(purchases);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(purchases);
    }

    [Authorize(Roles = 
   "Admin,DispensaryManager,DispensaryEmployee,DispensaryEastEmployee")]
    public ActionResult PurchaseIndex(string Id)
    {
        //this gets all purchases for a certain individual
        ApplicationDbContext db = new ApplicationDbContext();
        var userDetails = db.Purchases.Where(x => x.ApplicationUserId == 
    Id).ToList();
        ViewBag.UserID = Id;
        return View(userDetails);

    }

```

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Total Refactor\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*8

Here is the new controller in its entirety.

```
    public class PurchasesController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: Purchases
    public ActionResult Index()
    {
        var purchases = db.Purchases.Include(p => p.Users);
        return View(purchases.ToList());
    }

    // GET: Purchases/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Purchases purchases = db.Purchases.Find(id);
        if (purchases == null)
        {
            return HttpNotFound();
        }
        return View(purchases);
    }

    // GET: Purchases/Create
    public ActionResult Create()
    {
        ViewBag.Users = new SelectList(db.Users, "Id", "UserName");

        List<SelectListItem> selectListItems = new List<SelectListItem>();

        foreach (ApplicationUser user in db.Users)
        {
            SelectListItem selectListItem = new SelectListItem
            {
                Text = user.UserName,
                Value = user.Id.ToString()
            };
            selectListItems.Add(selectListItem);
        }
        //ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", 
   "UserName");
        return View();
    }

    // POST: Purchases/Create
    // To protect from overposting attacks, please enable the specific 
    properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = 
 "PurchaseId,Name,Points,TotalPoints,ApplicationUserId")] Purchases 
 purchases)
    {
        if (ModelState.IsValid)
        {
            var totalPoints = db.Purchases.Sum(x => x.Points);
            purchases.TotalPoints = totalPoints;
            db.Purchases.Add(purchases);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", 
     "UserName", purchases.ApplicationUserId);
        return View(purchases);
    }

    // GET: Purchases/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Purchases purchases = db.Purchases.Find(id);
        if (purchases == null)
        {
            return HttpNotFound();
        }
        ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", 
      "UserName", purchases.ApplicationUserId);
        return View(purchases);
    }

    // POST: Purchases/Edit/5
    // To protect from overposting attacks, please enable the specific 
      properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = 
  "PurchaseId,Name,Points,TotalPoints,ApplicationUserId")] Purchases 
  purchases)
    {
        if (ModelState.IsValid)
        {

            db.Entry(purchases).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", 
      "UserName", purchases.ApplicationUserId);
        return View(purchases);
    }

    // GET: Purchases/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Purchases purchases = db.Purchases.Find(id);
        if (purchases == null)
        {
            return HttpNotFound();
        }
        return View(purchases);
    }

    // POST: Purchases/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Purchases purchases = db.Purchases.Find(id);
        db.Purchases.Remove(purchases);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

```

} 

Now there is a dropdown list of users to choose from when you create a new purchase. Here is the Create View.

```
        <div class="col-12 backgroundImg navbarSpace scrollBar">
  <div class="formBackground col-12">
    <h2 class="formHeader">Edit Puchase</h2>
    <hr />
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">

            @Html.ValidationSummary(true, "", new { @class = "text-danger" 
     })
            @Html.HiddenFor(model => model.PurchaseId)
            @Html.HiddenFor(model => model.TotalPoints)

            <div class="col-12">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { 
    @class = "formText col-12" })
                <div class="col-12">
                    @Html.EditorFor(model => model.Name, new { 
     htmlAttributes = new { @class = "col-12" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new 
   { @class = "text-danger" })
                </div>
            </div>

            <div class="col-12">
                @Html.LabelFor(model => model.Points, htmlAttributes: new { 
     @class = "formText col-12" })
                <div class="col-12">
                    @Html.EditorFor(model => model.Points, new { 
    htmlAttributes = new { @class = "col-12" } })
                    @Html.ValidationMessageFor(model => model.Points, "", 
    new { @class = "text-danger" })
                </div>
            </div>

            @*<div class="col-12">
                    @Html.LabelFor(model => model.TotalPoints, 
     htmlAttributes: new { @class = "formText col-12" })
                    <div class="col-12">
                        @Html.EditorFor(model => model.TotalPoints, new { 
  htmlAttributes = new { @class = "col-12" } })
                        @Html.ValidationMessageFor(model => 
  model.TotalPoints, "", new { @class = "text-danger" })
                    </div>
                </div>*@

   <div class="col-12">@Html.LabelFor(model => model.ApplicationUserId, 
   "Users", htmlAttributes: new { @class = "formText col-12" })
    <div class="col-12"> @Html.DropDownList("Users", null, htmlAttributes: 
    new { @class = "col-12" })
     @Html.ValidationMessageFor(model => model.ApplicationUserId, "", new { 
          @class = "text-danger" })
                </div>
            </div>

     <div class="col-12">
         <div class="col-md-offset-2 col-md-10">
           <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div class="formText"> @Html.ActionLink("Back to List", "Index")
    </div>
</div>
</div>  

```

This creates a drop down list of users displaying their User Name. When I select a user and hit save I get an error saying that 
There is no ViewData item of type 'IEnumerable' that has the key 'Id'.