blog

Home / DeveloperSection / Blogs / Passing multiple dropdown list values from view to controller and saving into database.

Passing multiple dropdown list values from view to controller and saving into database.

Jonas Stuart9535 04-Feb-2017

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

Passing multiple dropdown list values from view to controller and saving into database.

And the click generate From Database

Passing multiple dropdown list values from view to controller and saving into database.

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

Passing multiple dropdown list values from view to controller and saving into 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 

Passing multiple dropdown list values from view to controller and saving into database.

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
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        publicActionResult Index()
        {
            var context = newDropdownEntities();
            ViewBag.country = context.Countries;
            ViewBag.state = context.States;
            ViewBag.district = context.Districts;
            return View();
           
        }
        [HttpPost]
        publicActionResult Add(Registration Model)
        {
            using(var context=newDropdownEntities())
            {
                context.Registrations.Add(Model);
                context.SaveChanges();
            }
            var context1 = newDropdownEntities();
            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;
}
 
<!DOCTYPEhtml>
 
<html>
<head>
    <metaname="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, newSelectList(ViewBag.country,"country1","country1"),"Select")</td>
                   </tr>
               <tr>
                   <td>
                     State:<br/>
                       @Html.DropDownListFor(x=>x.State,newSelectList(ViewBag.state,"state1","State1"),"Select")
                                          </td>
                   <td>District:@Html.DropDownListFor(x =>x.District, newSelectList(ViewBag.district,"district1","district1"),"Select")</td>
                   </tr>
              
               </table>
                &nbsp;<inputtype="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 that what value have posted in the form.

Passing multiple dropdown list values from view to controller and saving into database.

View page Look like


Passing multiple dropdown list values from view to controller and saving into database.



Updated 17-Mar-2018

Leave Comment

Comments

Liked By