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)
- {
-
- var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
-
- return userIdentity;
- }
- }
CreateView
- @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"> @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.
