Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
How can I Pass the Value from the View to Controller i.e.
I have a static Dropdown which I have created as follows.

1) Controller Code

C#
var list = new SelectList(new[] 
                {
                    new { ID = "1", Name = "name1" },
                    new { ID = "2", Name = "name2" },
                    new { ID = "3", Name = "name3" },
                },
                "ID", "Name", 1);

                ViewData["list"] = list;


2) In My View I am binding the Dropdown as follows:
@Html.DropDownList("list", ViewData["list"] as SelectList)

how can I get the selected value of the dropdown from view and pass it to a controller?

I tried the following but it did not work.
C#
string sTest = Request.Form["list"].ToString();


What I have tried:

I tried the following but it did not work.
string sTest = Request.Form["list"].ToString();
Posted
Updated 12-Mar-16 8:16am

1 solution

Put it in a form and submit the form using a submit button. This is a very basic part of mvc, you need to understand the basics first and a forum is not a good place to learn something from scratch. Get a book on MVC or at least go through some on-line tutorials and they'll cover these things, google "mvc music store" for a good starter walkthrough.
 
Share this answer
 

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