I'm new into MVC and I'm having a hard time solving this following issue. I got a userID via Session["LoggedUserID"]
as it get once the user log in. I want to pass it to following cshtml code(or directly in controller?) which I do not understand for the moment. This action occur once user want to create a new post.
inside cshtml view(code created automatically from Controller):
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CreatorUserId, "CreatorUserId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CreatorUserId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CreatorUserId, "", new { @class = "text-danger" })
</div>
</div>
My controller code:
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Review review)
{
if (ModelState.IsValid) {
db.Reviews.Add(review);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(review);
}
How do I pass Session["LoggedUserID"]
to cshtml (or directly via controller)? Review table is using userID as FK for user ID.
EDIT: The error message I get for my current code is:
There is no ViewData item of type 'IEnumerable' that has the key 'CreatorUserId'.
Any help is much appreciated. Thanks.