❔ Session Variables

Storing what a user selects from a dropdown to a session variable but it gives a null error which should not be. looked for the error i know it lies here. tried for 2 days but no success. Just wondering is there another way to achieve my goal?
cs [HttpPost]
public IActionResult Index(Student_List studentss)
{
int Studnentid = studentss.Stid;//problem is it is not showing the id or storing the id
HttpContext.Session.SetInt32("Stid", Studnentid);//if i change studentid to 1 it works
string studentname = studentss.StName;
HttpContext.Session.SetString("StName", studentname);
return RedirectToAction("Index", "Dashboard");
}
}
}
cs [HttpPost]
public IActionResult Index(Student_List studentss)
{
int Studnentid = studentss.Stid;//problem is it is not showing the id or storing the id
HttpContext.Session.SetInt32("Stid", Studnentid);//if i change studentid to 1 it works
string studentname = studentss.StName;
HttpContext.Session.SetString("StName", studentname);
return RedirectToAction("Index", "Dashboard");
}
}
}
cs
@using (Html.BeginForm("Index", "Profile", FormMethod.Post))//new
{
<div class="form-control">
<div class="col-md-3">
@using (Html.BeginForm("", "", FormMethod.Post))
{
@Html.DropDownListFor(model => model.studlist, new SelectList(Model.studlist, "Stid", "StName")

, new { @class = "form-control" })
}
</div>
<button type="submit" class="btn btn-primary">Choose</button>
</div>}
cs
@using (Html.BeginForm("Index", "Profile", FormMethod.Post))//new
{
<div class="form-control">
<div class="col-md-3">
@using (Html.BeginForm("", "", FormMethod.Post))
{
@Html.DropDownListFor(model => model.studlist, new SelectList(Model.studlist, "Stid", "StName")

, new { @class = "form-control" })
}
</div>
<button type="submit" class="btn btn-primary">Choose</button>
</div>}
11 Replies
Pobiega
Pobiega2y ago
And you have enabled sessions in your startup?
cake_combat
cake_combat2y ago
Yes I have enabled sessions in my program.cs
Pobiega
Pobiega2y ago
So by the comment "//if i change studentid to 1 it works", you mean if you hardcode the value to 1, what exactly works? that the session variable is updated? and if you don't, you get a null ref? on that line?
cake_combat
cake_combat2y ago
It displays the person's id or name in another controller/index
Pobiega
Pobiega2y ago
Okay, so there is nothing wrong with the session storage at all
cake_combat
cake_combat2y ago
Yes
Pobiega
Pobiega2y ago
its with your view model being weird My first suggestion would be to actually stick to a naming standard, preferably the official C# one Student_List is a bad class name, and even worse considering it doesnt seem to actually represent a list. (Should be StudentList, PascalCase). studentss has a typo in it. int Studnentid should be int studentId, no typo, camelCase. string studentname = studentss.StName; should be string studentName = students.StudentName;. camelCase local variable, no shortened name on the property. then, use the debugger to actually inspect the value you are getting for studentss. It appears to be null, which would mean the model binding failed or your frontend didnt actually send and form data.
cake_combat
cake_combat2y ago
Student_list is a list in the model. Thank you so much i would work on the naming Ohhh thank you
Pobiega
Pobiega2y ago
ah, but for a [HttpPost] endpoint, your method should take a single argument, a class object that represents the entire form payload
cake_combat
cake_combat2y ago
I understand, i would implement the changes and pray it finally Smadge
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.