Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
Suppose if i have a dropdown in a view page and if i select any value in the dropdown and the selected value needs to be passed to the different view.

please let me know how is it possible.

Thanks in Advance.
Posted
Updated 23-Oct-18 15:50pm
v2

Does the user have to "submit" the page for it to constitute the value being being persisted, or does the user simply have to select an option and then navigate to another page?

Anyway, you could just use the session storage. You can set this on a page submit (POST) or use some javascript Ajax to send the value on the change event of the drop down.

1. The POST way

(in your controller)

[HttpPost]
public ActionResult MyPage(PageData pageData)
{
   Session["SelectedValue"] = pageData.MyDropDownValue;
}


2. The AJAX way (using JQuery)

(in your controller)

[HttpPost]
public ActionResult UpdateSession(string selectedValue)
{
   Session["SelectedValue"] = selectedValue;
}



(in your javascript)

$(function () {
   $("#MyDropDown").change(onChange);
};

function onChange(){
   $.ajax({
        url: 'MyController/UpdateSession',
        type: 'POST',
        dataType: 'json',
        data: {selectedValue: $("#MyDropDown").val()},
        contentType: 'application/json; charset=utf-8',
        success: function(data){  },
        error: function(err) { alert(err); }
    });
}


then in the page you want to load the data you can access the session with...

<%= Session["SelectedValue"] %>


Or with the Razor View Engine in MVC 3 you could use the ViewBag instead...

(controller)

ViewBag.SelectedValue = "My Value";


(cshtml page)

@(ViewBag.SelectedValue)
 
Share this answer
 
Comments
aarif moh shaikh 24-Sep-15 1:16am    
good one... agree with you :)
Member 12070444 10-Jan-16 14:55pm    
Thanks, I have similar problem however mine is to a stonghly type textbox to another view.
The below code needs to be Void because it dosen't return anything.
I hope this helps someone.
C#
[HttpPost]
public ActionResult UpdateSession(string selectedValue)
{
   Session["SelectedValue"] = selectedValue;
}
 
Share this answer
 
Comments
CHill60 23-Sep-15 11:40am    
Adds nothing to solution 1 posted over 4 years ago

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900