Referencing Foreign key to IdentityUser. Table with FK Shows Null after Inserting Data.
I'm quite new to ASP.NET MVC. What I've made is a Register and Login from the Scaffolding Views.
Another table I've made is "Feedback" and it has a foreign key "UserId" referencing to ASP.Net Identity User which is the "Id" field. Once I've done creating a Create() View for the feedback, it has been successfully saving the 2 fields that I want but not the UserId, which just shows null.
How do I even work with this to reference the Id of the current user to the feedback table.
Here are my code
So my main question is: How, and what can I do to automatically insert the UserId ForeignKey to my Feedback table, because what I want to happen is whenever a logged in user is posting a feedback, I also want to link their IdentityUser Id to my Feedback table in the form of a foreignkey UserId.
Feedback Model
public class Feedback
{
[Key]
public int FeedbackId { get; set; }
[Required(ErrorMessage = "Subject Required.")]
public string Subject { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Text { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
public ICollection<Feedback> Feedbacks { get; set; }
}
FeedbackController for creating a Feedback
[HttpPost]
public IActionResult Create(Feedback record)
{
var item = new Feedback()
{
Subject = record.Subject,
Text = record.Text,
};
_context.Feedbacks.Add(item);
_context.SaveChanges();
return RedirectToAction("Index");
}