I am using Entity Framework and MVC with Individual User Accounts and I have a table called "Kit" that has a UserId column which is linked to the AspNetUser 'ID' as the foreign key.
When I go to create a new kit and save to the database I want the 'UserId' of my Kit table to be the current ASPNetUser that is logged in.
But currently when I create the new Kit object it just sets the UserId to NULL and never picks up the current user logged in.
What am I missing here?
Controller Create()
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public ActionResult Create([Bind(Include = "KitId,KitName,ProductId,UserId")] Kit kit)
{
if (ModelState.IsValid)
{
db.Kits.Add(kit);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductId = new SelectList(db.Products, "ProductId", "ProductName", kit.ProductId);
ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", kit.UserId);
return View(kit);
}
View
<div class="form-group">
@Html.LabelFor(model => model.AspNetUser.Id, "User", htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.UserId, "Email", new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
</div>
</div>
Here is the row in my database and you can see UserId was never populated
KitId KitName ProductId Available LoanId UserId
3 TestKit 12 NULL NULL NULL