---
title: "Passing multiple dropdown list values from view to controller and saving into database."  
description: "For doing this task we use data first approach entity framework. In this first we create database and then generate model on basis of database  First"  
author: "Jonas Stuart"  
published: 2017-02-04  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11290/passing-multiple-dropdown-list-values-from-view-to-controller-and-saving-into-database  
category: "asp.net mvc"  
tags: ["mvc"]  
reading_time: 3 minutes  

---

# Passing multiple dropdown list values from view to controller and saving into database.

For doing this task we use [data first approach](https://www.mindstick.com/forum/34493/call-stored-procedures-with-entity-framework-6-data-first-approach-in-asp-dot-net-mvc-4) [entity framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach). In this first we create [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) and then generate model on basis of database.\

**First we create database and table**

**Countrylist table for first [dropdown](https://www.mindstick.com/articles/263/ajax-toolkit-dropdownextender-in-asp-dot-net)**

```
CREATE TABLE [dbo].[Country](          [Id] [int] IDENTITY(1,1) primary key NOT NULL,          [Country] [varchar](50) NULL,          )                   DistrictList tale         CREATE TABLE [dbo].[District](          [Id] [int] IDENTITY(1,1) primary key NOT NULL,          [District] [varchar](50) NULL,          )And Statelist table CREATE TABLE [dbo].[State](          [Id] [int] IDENTITY(1,1) primary key NOT NULL,          [State] [varchar](50) NULL,          )
```

## And Registrationform table

```
CREATE TABLE [dbo].[Registration](          [Id] [int] IDENTITY(1,1) primary key NOT NULL,          [Name] [varchar](50) NULL,          [Country] [varchar](50) NULL,          [State] [varchar](50) NULL,          [District] [varchar](50) NULL,          [Address] [varchar](50) NULL,) 
```

**Now add New Project and Right Click on the Models folder and select [Ado.Net](https://www.mindstick.com/articles/804/connection-pooling-in-ado-dot-net) [Entity Data Model](https://www.mindstick.com/articles/594/ado-dot-net-entity-data-model-in-wpf)**

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/51a71c50-76d7-47b1-9a44-7995e37b6f0c.png)\

**And the click generate From Database**

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/8ca31d3a-8cf7-4277-9ef8-471332c0d26e.png)\

**And next and then give [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) information and select your database.**

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/06da6afa-9b0e-43da-9b3c-5da78da9c7fa.png)\

**Then choose tables and then finish.**

In the next step we will create Home Controller for adding controller right click on the controllers folder and go to add and then click controller then a pop up will be appear from here you can change you can give your controller name \

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/24470ca1-55d7-45fb-b8e8-48a191483781.png)\

**In the [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) copy the following code.**

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;       using MvcApplication4.Models; namespace
MvcApplication4.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/         public ActionResult Index()        {            var context = new DropdownEntities();            ViewBag.country = context.Countries;            ViewBag.state = context.States;            ViewBag.district = context.Districts;            return View();                    }        [HttpPost]        public ActionResult Add(Registration Model)        {            using(var context=new DropdownEntities())            {               
context.Registrations.Add(Model);                context.SaveChanges();            }            var context1 = new DropdownEntities();            ViewBag.country = context1.Countries;            ViewBag.state = context1.States;            ViewBag.district =context1.Districts;            return View("Index");        }     }}
```

## And in the view

```
@model MvcApplication4.Models.Registration@{    Layout = null;} <!DOCTYPE html> <html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title>    <style>        select {        width:200px}        input {        width:200px}            input[type="Submit"] {                color:blue;                width:80px;            }        fieldset {            width:500px;            margin-left:250px;        }    </style></head><body>    <div>       @using (Html.BeginForm("Add","Home",FormMethod.Post))       {                       <fieldset>               <legend><strong>Registration Form</strong></legend>           <table>               <tr><td>                   Name:                  @Html.TextBoxFor(x=>x.Name)</td>                   <td>Country:@Html.DropDownListFor(x
=> x.Country, new SelectList(ViewBag.country,"country1","country1"),"Select")</td>                   </tr>               <tr>                   <td>                     State:<br />                       @Html.DropDownListFor(x=>x.State,new SelectList(ViewBag.state,"state1","State1"),"Select")                                          </td>                   <td>District:@Html.DropDownListFor(x =>x.District, new SelectList(ViewBag.district,"district1","district1"),"Select")</td>                   </tr>                              </table>                &nbsp; <input type="submit" value="Submit" />               </fieldset>       }    </div></body></html>
```

**For testing add break point at the add method .when we fill the form submit then we can see in the Model [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) that what value have posted in the form.**

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/a7c63368-840f-4af2-a964-df38dcb10469.png)\

View page Look like

\

![Passing multiple dropdown list values from view to controller and saving into database.](https://www.mindstick.com/blogs/a4ff86ed-8d8d-4210-b9ca-d52de131ab78/images/241b8f37-18cc-41a9-a342-59967466961c.png)\

\

---

Original Source: https://www.mindstick.com/blog/11290/passing-multiple-dropdown-list-values-from-view-to-controller-and-saving-into-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
