Reputation: 69
currently work on mvc with using razor, where wish to assign my session value to model, may i know how it can be done? did some research but din see any.
to display the ID of student that applying for password recovery, and save the studentId in session in order to further process and show in password recovery form in view
@Html.LabelFor(m => m.StudentId)
@Html.DisplayFor(m=> m.StudentId)
when i wish to assign my session into m.StudentId, is there anyway can solve it?
in controller
Session["StudentId"] = passwordrecovery.StudentId;
Upvotes: 2
Views: 830
Reputation: 33865
If I understand you right, the best way would probably be to create a viewmodel, to which you assign the StudentId in your action before you return the view, rather than keeping the value in the session. You then pass the viewmodel to the view, and will have access to the StudentId in your view when creating your form.
// Create a view model that fit your needs
public class PasswordRecoveryViewModel {
public int StudentId { get; set; }
// Other properties as needed
}
// Do something like this in your action
public ActionResult YourAction () {
var model = new PasswordRecoveryViewModel {
StudentId = 1; // Assign the ID as needed
}
return View(model);
}
Then at the top of your view you add this:
@Model YourNameSpace.PasswordRecoveryViewModel;
You will then have access to your student ID with @Html.LabelFor(m => m.StudentId)
Upvotes: 1
Reputation: 7591
you do not need to store the id in session. use the built-in membership provider functions to manage the user account.
beyond that I would recommend loading anything/everything you need for the view into the viewbag and not rely on other external sources for data. this can be done using ActionFilters to pull values out of sources (like Sesssion) and place them into the ViewBag or View Model.
Upvotes: 1