Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii Everyone..
I want to send the select item from a drop down list to the database in asp.net mvc.

my only requirement is that how can all the values of drop down list {male, female}
will get save into the database.

I m new to MVC so please suggest me simple way to solve this issue
thnx in advance

What I have tried:

This is View



@{var listItems = new List<SelectListItem>
{
new SelectListItem {Text="Select Your Gender", Value="1"},
new SelectListItem {Text="Male", Value="2"},
new SelectListItem {Text="Female", Value="3"}

};
}




This is My HomeController


public ActionResult Create(Student s)
        {
            Sample dbContext = new Sample();
            var v = dbContext.Students.Add(s);
            dbContext.SaveChanges();
            return RedirectToAction("GetAllStudent");
        }
Posted
Updated 11-Aug-17 3:45am
v2
Comments
F-ES Sitecore 11-Aug-17 8:43am    
Only the selected item is included in a form submission so if you want to know what the non-selected values are you'll need to re-create the data yourself. As your list appears to be hard-coded it's fairly easy to work out what the non-selected values are.
Rohit Singh 11-Aug-17 8:49am    
can you please improve my code
F-ES Sitecore 11-Aug-17 8:55am    
You haven't explained what it is you want to do.
Rohit Singh 11-Aug-17 9:01am    
i just want to send the data from drop down list to database..if you can give me any example i'll be very thankful to you

1 solution

From your description it is not clear what you are trying to do. However, considering you are trying to bind some list to dropdown and save selected value to database, refer and try the below.

ASP.Net MVC: Get DropDownList Selected Text and Selected Value in Controller
 
Share this answer
 
Comments
Rohit Singh 15-Aug-17 7:54am    
okie.. thnx

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