Click here to Skip to main content
14,265,982 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to set selected item from dropdown to Session in MVC5 ,so that it can be used anywhere in the application for the current user, Kindly let me know is there any other workaround?

What I have tried:

@Html.DropDownList("CompanyList", ViewBag.CompanyList as SelectList,
"Select a Company", new { id = "CompanyId" })



<input type="button" value="Go" id="btnSelect"
/>
@section scripts
{
<script>
$("#btnSelect").click(
function ()
{
var value = document.getElementById("CompanyId").value;
@Context.Session["CompanyId"]="'"+value+"'";
alert( "Session "+ @Context.Session["CompanyId"].ToString());
}
);
</script>
}


but this returns expression being session points to NUll
Posted
Comments
F-ES Sitecore 22-Mar-16 7:05am
   
You have to send the data to your server code to store it in the session. Do this in the controller when the form is submitted, or if it's not part of a form submission then make an ajax call to a controller to set the value in the session. Google "jquery ajax call mvc" for more info, or you can use the built-in ajax helper if you're using unobtrusive javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100