TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
[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;
}
}
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.
Reply
Answers (
2
)
How to code ASP.Net MVC of Signup and Register module?
How to add Master Page in MVC 4?