Thabo Happy

Thabo Happy

  • NA
  • 105
  • 15.9k

asp.net mvc 5 wont save foreign key

Sep 14 2018 3:57 AM
Hello i want do save data with specific user id but am stack i don't know how to do it, i was told i could set my post method to insert data with current user id as a foreign key but i have no idea how to di it. so i have two tables aspNetUsers and Tutorial the relationship is one to may, a user can have multiple tutorials
 
Controller
  1. [HttpPost]  
  2. [AllowAnonymous]  
  3. [ValidateAntiForgeryToken]  
  4. public ActionResult Create(Tutorial Tutorial, string UserId)  
  5. {  
  6. if (ModelState.IsValid)  
  7. {  
  8. db.Tutorials.Add(Tutorial);  
  9. db.SaveChanges();  
  10. return RedirectToAction("Index");  
  11. }  
  12. return View(Tutorial);  
  13. }
Tutorialmodel
  1. [Table("Tutorial")]
  2. public class Tutorial
  3. {
  4. [Key]
  5. public int TutorialId { get; set; }
  6. public string Topic { get; set; }
  7. [Required(ErrorMessage = "Course Name is required")]
  8. [Display(Name = "Course Name")]
  9. public string CoursesName { get; set; }
  10. [Required(ErrorMessage = "Discription is required")]
  11. public string Description { get; set; }
  12. [AllowHtml]
  13. public string Content { get; set; }
  14. [ForeignKey("User")]
  15. public string UserId { get; set; }
  16. public virtual ApplicationUser User { get; set; }
  17. }
IdentityModels
  1. public class ApplicationUser : IdentityUser
  2. {
  3. public virtual ICollection Tutorials { get; set; }
  4. public async Task GenerateUserIdentityAsync(UserManager manager)
  5. {
  6. // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
  7. var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
  8. // Add custom user claims here
  9. return userIdentity;
  10. }
  11. }
CreateView
  1. @using (Html.BeginForm())  
  2. {  
  3. @Html.AntiForgeryToken()  
  4. class="form-horizontal">  
  5. Tutorial

      
  6.   
  7. @Html.ValidationSummary(true""new { @class = "text-danger" })  
  8. class="form-group">  
  9. @Html.LabelFor(model => model.Topic, htmlAttributes: new { @class = "control-label col-md-2" })  
  10. class="col-md-10">  
  11. @Html.EditorFor(model => model.Topic, new { htmlAttributes = new { @class = "form-control" } })  
  12. @Html.ValidationMessageFor(model => model.Topic, ""new { @class = "text-danger" })  
  
  •   
  • class="form-group">  
  • @Html.LabelFor(model => model.CoursesName, htmlAttributes: new { @class = "control-label col-md-2" })  
  • class="col-md-10">  
  • @Html.EditorFor(model => model.CoursesName, new { htmlAttributes = new { @class = "form-control" } })  
  • @Html.ValidationMessageFor(model => model.CoursesName, ""new { @class = "text-danger" })  
  •   
  •   
  • class="form-group">  
  • @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })  
  • class="col-md-10">  
  • @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })  
  • @Html.ValidationMessageFor(model => model.Description, ""new { @class = "text-danger" })  
  •   
  •   
  • class="form-group">  
  • @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })  
  • class="col-md-10">  
  • @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })  
  • @Html.ValidationMessageFor(model => model.Content, ""new { @class = "text-danger" })  
  •   
  •   
  • @*@Html.HiddenFor(model => model.User.Id)*@  
  • @*class="form-group">  
  • @Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })  
  • class="col-md-10">  
  • @Html.DropDownList("UserId"null, htmlAttributes: new { @class = "form-control" })  
  • @Html.ValidationMessageFor(model => model.UserId, ""new { @class = "text-danger" })  
  •   
  • *@  
  • class="form-group">  
  • class="col-md-offset-2 col-md-10">  
  • "submit" value="Create" class="btn btn-default" />  
  •   
  •   
  •   
  • }   
  • i would apreciate any help.


    Answers (2)