2

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.

13
  • You can access the session in the controller, so it's not clear what the actual problem is. Commented Sep 26, 2016 at 18:10
  • 2
    Possible duplicate of Access session on view page in ASP.Net MVC Commented Sep 26, 2016 at 18:11
  • 1
    Or stackoverflow.com/questions/18608452/… ... Really unclear what you have problem with (possibly because you don't understand how ASP.Net MVC works or maybe how Session works). Commented Sep 26, 2016 at 18:19
  • 1
    What you mean "pass Session to Cshtml" ? You want to pass so that you can display it in a label ? or something else ? You have to be clear about what you are doing. Also your current code is using the DropDownList helper method and i do not see any ViewData set in your GET action for populating the dropdown (that is the reason you are getting the "There is no ViewData...." error message! Commented Sep 26, 2016 at 18:29
  • 1
    @skylake See the answer i posted Commented Sep 26, 2016 at 18:57

2 Answers 2

2

If it is for saving with entity, there is no need to pass it to the view and send it back to server. You can use it directly in your HttpPost action method.

[HttpPost]
public ActionResult Create(Review review)
{  
    if (ModelState.IsValid) 
    {
        if(Session["LoggedUserID"]==null)
        {
           return Content("Session LoggedUserID is empty");
        }

        review.UserID = Convert.ToInt32(Session["LoggedUserID"]);   
        db.Reviews.Add(review);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(review);        
}

I also added an If condition to check whether Session["LoggedInUserID"] is null before reading it to an int variable( UserID property). Ideally you may move this out of the action method to check whether user is logged in or not (May be an action filter like this)

Sign up to request clarification or add additional context in comments.

3 Comments

I still getting same error: There is no ViewData item of type 'IEnumerable' that has the key 'CreatorUserId'. at the line -> @Html.DropDownList("CreatorUserId", null, htmlAttributes: new { @class = "form-control" }). Since I use guid instead int for userID I had to change review.UserID = Convert.ToInt32(Session["LoggedUserID"]); to Guid guidSess = new Guid(Session["LoggedUserID"].ToString()); and pass guidSess to review.CreatorUserId = guidSess;. Was this done correctly? Just in case this caused the error.
No. Your error is because your view code is using the DropDownList helper method which expects you to set the data needed to populate the dropdown to Viewdata dictionary. Since there is no need to show the dropdown, simply remove outer div which contains the line @Html.DropDownList and ValidationMessageFor from your view
Aah I see! You mentioned it earlier but I accidentally overlooked it. Thank you, I really learned a lot from this.
1

You need to bind the value i.e. "User ID" to view model or put it in view bag and you can access that in the cshtml.

E.g.

SampleViewModel vm = new SampleViewModel(){UserId = yourvalue}; 

You need to create this SampleViewModel class with a property called UserId and then you can use like this. You need to bind the view model to your cshtml also like - @model SampleViewModel.cs

or you can store the value in view bag. i.e.

ViewBag.UserId = your-value; //You can call @ViewBag.UserId in cshtml page. 

Does this help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.