Add a Parameter to Index.cshtml to Read the Selected Option Value

Introduction

There are many means in which we tin populate a DropDownList control with data. In this article, I will demonstrate the simple ways to populate a DropDownList using ViewBag, ViewData, TempData, jQuery, Model, Database, jQuery AJAX, and hardcoding in View.

Article Flow

  • Populate a DropdownList using Hardcoded data in view
  • Populate a DropdownList using Viewbag
  • Populate a DropdownListusing ViewData
  • Populate a DropdownList using TempData
  • Populate a DropdownList using Enum
  • Populate a DropdownList using Database with Entity Framework
  • Populate a DropdownList using Jquery Ajax with JSON Data
  • Populate a DropdownList using Model
  • Populate a DropdownList using Global Static Information in View

Before moving on this, Create an empty ASP.NET MVC project and whenever we create a controller it creates an empty index activity. Add a view page for this index activity past right clicking on Alphabetize(controller action) and adding View (Index). If you are non aware of the process to create ASP.Internet MVC empty project, follow the steps 1 to 3 hither.

Hither, I mentioned a project name "VariousWayBindingDropDownListInMVC5" and controller name is DropDownListController. Okay! let's see one by one.
Right now, the below i is our empty Controller.

          using System;     using System.Collections.Generic;     using Arrangement.Linq;     using System.Web;     using System.Web.Mvc;     namespace VariousWayBindingDropDownListInMVC5.Controllers {         public course DropDownListController: Controller {             //             // GET: /DropDownList/             public ActionResult Index() {                 return View();             }         }     }        

Populate a DropDownList using Hardcoded data in view

We can have a DropDownList bound with some static values in View itself. Simply add together an Html helper for DropDownList and provide the static list of SelectListItem. Now bind the hardcoded values to DropDownList as below in view

          <tr>   <td> Populating With Hardcoded Data</td>   <td>   @Html.DropDownList("MySkills", new List<SelectListItem>   {      new SelectListItem{ Text="ASP.NET MVC", Value = "1" },      new SelectListItem{ Text="ASP.NET WEB API", Value = "2" },      new SelectListItem{ Text="ENTITY FRAMEWORK", Value = "3" },      new SelectListItem{ Text="DOCUSIGN", Value = "4" },      new SelectListItem{ Text="ORCHARD CMS", Value = "5" },      new SelectListItem{ Text="JQUERY", Value = "6" },      new SelectListItem{ Text="ZENDESK", Value = "7" },      new SelectListItem{ Text="LINQ", Value = "8" },      new SelectListItem{ Text="C#", Value = "9" },      new SelectListItem{ Text="GOOGLE ANALYTICS", Value = "10" },   })   </td>   </tr>        

Now run your application, in below paradigm, you can see that DropDownList has populated with hardcoded data

Populate a DropDownList using Viewbag

To populate DropDownList using Viewbag, let's create some collection list with selectListItem types and assign to the Viewbag with appropriate name

          public ActionResult Index() {     #region ViewBag       List < SelectListItem > mySkills = new List < SelectListItem > () {           new SelectListItem {               Text = "ASP.Internet MVC", Value = "i"           },           new SelectListItem {               Text = "ASP.Cyberspace WEB API", Value = "ii"           },           new SelectListItem {               Text = "ENTITY FRAMEWORK", Value = "3"           },           new SelectListItem {               Text = "DOCUSIGN", Value = "4"           },           new SelectListItem {               Text = "ORCHARD CMS", Value = "v"           },           new SelectListItem {               Text = "JQUERY", Value = "6"           },           new SelectListItem {               Text = "ZENDESK", Value = "seven"           },           new SelectListItem {               Text = "LINQ", Value = "8"           },           new SelectListItem {               Text = "C#", Value = "9"           },           new SelectListItem {               Text = "GOOGLE ANALYTICS", Value = "ten"           },       };       ViewBag.MySkills = mySkills;       

0 Response to "Add a Parameter to Index.cshtml to Read the Selected Option Value"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel