How to pass multiple dropdown list values from view to controller and saving into a database?

I am having difficulty in passing multiple dropdown list values from view to controller and saving data into a database. Please help me.
Thanks in advance.
Last updated:5/4/2017 12:12:40 AM

1 Answers

Manish Kumar
Manish Kumar


First we create database and table

Countrylist table for first dropdown


CREATE TABLE [dbo].[Country](
          [Id] [int] IDENTITY(1,1) primary key NOT NULL,
          [Country] [varchar](50) NULL,
          )

DistrictList table

 

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 Entity Data Model




And the click generate From Database





And next and then give connection information and select your database.






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  .







In the controller 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>
 


View page Look like






Answer