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
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Create(Tutorial Tutorial, string UserId)
- {
- if (ModelState.IsValid)
- {
- db.Tutorials.Add(Tutorial);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(Tutorial);
- }
Tutorialmodel
- [Table("Tutorial")]
- public class Tutorial
- {
- [Key]
- public int TutorialId { get; set; }
- public string Topic { get; set; }
- [Required(ErrorMessage = "Course Name is required")]
- [Display(Name = "Course Name")]
- public string CoursesName { get; set; }
- [Required(ErrorMessage = "Discription is required")]
- public string Description { get; set; }
- [AllowHtml]
- public string Content { get; set; }
- [ForeignKey("User")]
- public string UserId { get; set; }
- public virtual ApplicationUser User { get; set; }
- }
IdentityModels
- public class ApplicationUser : IdentityUser
- {
- public virtual ICollection
Tutorials { get; set; } - public async Task
GenerateUserIdentityAsync(UserManager manager) - {
- // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
- var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
- // Add custom user claims here
- return userIdentity;
- }
- }
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- class="form-horizontal">
Tutorial
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- class="form-group">
- @Html.LabelFor(model => model.Topic, htmlAttributes: new { @class = "control-label col-md-2" })
- class="col-md-10">
- @Html.EditorFor(model => model.Topic, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Topic, "", new { @class = "text-danger" })
class
="form-group"> class
="col-md-10"> class
="form-group"> class
="col-md-10"> class
="form-group"> class
="col-md-10"> class
="form-group"> class
="col-md-10"> class
="form-group"> class
="col-md-offset-2 col-md-10"> 

Ahmed AbdiPosted Sep 17, 2018, 5:55 AM
Faisal PathanPosted Sep 17, 2018, 5:46 AM