How fetch data from database in DropDownList in MVC?

ALL DROPDOWNLIST FUN AND LEARN

The following is a snapshot of the binding of the Dropdownlist:

@Html.Label["Normal Dropdownlist Binding"]

@Html.Label["Select Mobile Name"]

@Html.DropDownList["Mobiledropdown1", Model.MobileList, "Select Mobile"]

Here we can directly access the MobileList from the Model.

Now just run the application and just check it.

It's done.

Second way to Bind Dropdownlist

Now in the second way we just need to pass the same list to the Viewbag.

As in the first way we have passed a value to the model now in the same way we would pass a list to the Viewbag.

@Html.Label["Dropdownlist Binding Using ViewBag"]

@Html.Label["Select Mobile Name"]

@Html.DropDownList["Mobiledropdown2", ViewBag.VBMobileList as SelectList, "Select Mobile"]

ViewBag.VBMobileList=newSelectList[MCon.GetMobileList[],"MobileID","MobileName"]; //Viewbag

For your reference you can run and check it.

Third way to Binding Dropdownlist

In the third way everything will be the same but the binding to the DropdownlistFor is different.

Using the same model that was used for the first way to do the binding .

MD.MobileList=newSelectList[MCon.GetMobileList[],"MobileID","MobileName"];

Here is a snapshot to show how to bind.

@Html.Label["Dropdownlist Binding Using Model [Lamda Expression]"]

@Html.Label["Select Mobile Name"]

@Html.DropDownListFor[M => M.MobileID, new SelectList[Model.MobileList, "Value", "Text"], "Select Mobile"]

For binding the dropdownlist we require a LINQ expression and IEnumreable list.

As you have seen if you are creating a view directly using the scafffloding technique then you can see a LINQ lamda expression.

For example. @Html.TextboxFor[m => m.MobileName]

Fourth way to Binding Dropdownlist

In the last way we can pass hardcoded values to the dropdownlist on the View only.

1. Directly View

@Html.Label["Dropdownlist Binding on View Directly"]

@Html.Label["Select Mobile Name"]

@Html.DropDownList["Mobiledropdown3", new List { new SelectListItem { Text = "HTC DESIRE", Value = "1"}, new SelectListItem { Text = "Moto G", Value = "2"}, new SelectListItem { Text = "GO mobiles", Value = "3"} }, "Select Mobile"]

2. Binding directly using ViewBag

The same Listthat we pass in the view directly can also be sent from the Controller and bound directly using a ViewBag.

@Html.Label["Dropdownlist Binding using SelectListitem and Viewbag"]

@Html.Label["Select Mobile Name"]

@Html.DropDownList["Dr", ViewData["MyhardcodeVal"] as List]

Now we completed the binding of the Dropdownlist.

Now you may have a question of how to read the Dropdownlist values.

You can read using a FromCollection or Model.

Here you need to create a Post Method .

If you want to read all the values of the dropdownlist or any HTML control then you will get in FormCollection.

Post method from MobileDisplayController:

[HttpPost] public ActionResult Index[FormCollection objfrm, Mobiledata objMd] { string mobile1 = objfrm["Mobiledropdown1"]; string mobile2 = objfrm["Mobiledropdown2"]; string mobile3 = objfrm["Mobiledropdown3"]; return View[objMd]; }

How to set a selected value of Dropdownlist on EditPage

Here I am showing how to show a selected dropdownlist value on Edit Page because this small thing will take time when you are new to this kind of technology.

Get the method of the Edit page from MobileDisplayController.

Output after editing.

How to add a Select at the top of the selection list.

Just add a String value at the end of the Dropdownlist.

@Html.DropDownList["Mobiledropdown1",Model.MobileList,"SelectMobile"]

Enjoy programming and Enjoy Sharing.

Introduction

This guide presents a couple of common ways to populate dropdown lists in ASP.NET MVC Razor views, with an emphasis on producing functional HTML forms with a minimum amount of code. It is intended to help developers who are working to improve their proficiency with some key technologies.

Also shown is how the contents of one dropdown list can be updated dynamically based on the selection made in another dropdown list.

Skill Levels

It will be helpful to have an understanding of the following topics, but an extensive working knowledge isn't required.

TechnologySkill Level
AjaxIntroductory
ASP.NETBeginner
C#Beginner
Entity FrameworkBeginner
JavaScriptBeginner
jQueryIntroductory
MVCBeginner

Scope

This guide focuses on ASP.NET MVC following the model-view view-model [MVVM] design pattern. The solution includes a small amount of JavaScript utilizing the jQuery library, in addition to the required C# code. Entity Framework is used to handle the interface between the SQL Server database and the data entities in the model.

Structure

  1. We'll start with the essential code for implementing a dropdown list populated when the view model is created.

  2. Building on that functionality, we'll look at how to populate the State/Province/Region dropdown based on the country selected.

  3. Next, we'll look at the entities underlying the case study and how the pieces fit together.

  4. Finally, we'll touch on some considerations for building production solutions.

Case Study

A complete Visual Studio solution with all the code shown in this guide is available from GitHub.

Conventions

In each of the code segments below we'll see the file name from the associated sample project, like this:

BlipDrop.csproj

We'll also see the using statements so you can better understand how the components reference each other.

Ellipsis [...] in the code segments indicate that in the complete file there is code above or below the segment that is unrelated to the topic at hand.

Video liên quan

Bài mới nhất

Chủ Đề