Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 10-Jul-11 23:17pm
siva455793
Edited 10-Jul-11 23:24pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Article helped me. Thanks
  Permalink  
Comments
Maciej Los at 26-Feb-14 11:20am
   
This is not an answer. Please delete it to avoid down-voting.

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 156
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 26 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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