Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,
please help me how to access selected value from the dropdownlist and to insert value into database in Asp.net MVC

In controller:
C#
List<int> li = new List<int>();
            for (int i = 20; i < 50; i++)
            {
                li.Add(i);

            }
            ViewData["j"] = new SelectList(li);

In view
C#
<%=Html.LabelFor(Model=>Model.age) %>
                <%=Html.DropDownList("j")%>

In model
C#
public class details
{
[DisplayName("Age")]
        public int age { get; set; }

}
Posted
Updated 1-Jul-13 23:15pm
v2

You can always get the data from Request object at HttpPost
eg.
C#
var value1 = Request["SimpleProp1"];
var value2 = Request["SimpleProp2"];
var value3 = Request["ComplexProp1.SimpleProp1"];
 
Share this answer
 
C#
<![CDATA[<%=Html.DropDownList(Model=>Model.age)%>]]>



C#
public Actionview PostAction(Model model)
{
    //selected combobox value
    int age = model.age;
}
 
Share this answer
 
In View You have to do something like this

C#
@Html.DropdownListFor(Model=>Model.age,new SelectListItem{@ViewBag.j,DataFieldId,DataFieldValue},"Select Text");



i have shown you genral format,use like this. This will surely help you.
 
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