Click here to Skip to main content
12,951,606 members (56,045 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
List<int> li = new List<int>();
            for (int i = 20; i < 50; i++)
            {
                li.Add(i);
 
            }
            ViewData["j"] = new SelectList(li);
 

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

In model
public class details
{
[DisplayName("Age")]
        public int age { get; set; }
 
}
Posted 1-Jul-13 21:11pm
Updated 1-Jul-13 23:15pm
vijay__p5.8K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can always get the data from Request object at HttpPost
eg.
var value1 = Request["SimpleProp1"];
var value2 = Request["SimpleProp2"];
var value3 = Request["ComplexProp1.SimpleProp1"];
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

<![CDATA[<%=Html.DropDownList(Model=>Model.age)%>]]>



public Actionview PostAction(Model model)
{
    //selected combobox value
    int age = model.age;
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

In View You have to do something like this

@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.
  Permalink  

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
OriginalGriff 6,084
CHill60 3,480
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,830


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 Jul 2013
Copyright © CodeProject, 1999-2017
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