Click here to Skip to main content
12,065,756 members (33,814 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# 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 11-Jul-11 0:17am
siva455819
Edited 11-Jul-11 0:24am
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  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The below code needs to be Void because it dosen't return anything.
I hope this helps someone.
[HttpPost]
public ActionResult UpdateSession(string selectedValue)
{
   Session["SelectedValue"] = selectedValue;
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160207.1 | Last Updated 23 Sep 2015
Copyright © CodeProject, 1999-2016
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