Here I will explain how to set default selected value for dropdownlist in asp.net
mvc razor
with example or asp.net
mvc @Html.Dropdownlist
set default selected value in razor with example or asp.net
mvc set
dropdownlist selected value with example. In asp.net
mvc by
using @Html.Dropdownlist or @Html.DropdownlistFor properties we can
implement dropdownlist and we can easily set dropdownlist default selected
value using @Html.Dropdownlist
property.

Here
“DropDownList” function will render it as
<select> with <options> tags and “ddlUsers” is used to set id and name
for dropdownlist and “Model.usersinfo”
will provide all possible options for dropdown and “2” will set default selected value for dropdownlist. Once above
dropdown render into html browser that will be like as shown below.

We
will see how to bind dropdownlist values from database and how to set dropdownlist
default selected value with example in asp.net
mvc.

Generally,
in asp.net mvc we can implement
dropdownlist by using two properties either @Html.DropDownList model or @Html.DropDownListFor
model. Before we start implementing first design userdetails table in database and insert some data like as shown
below.

Column
Name

Data
Type

Allow
Nulls

userid

Int(IDENTITY=TRUE)

NO

username

varchar(50)

Yes

education

Varchar(50)

Yes

location

Varchar(50)

Yes

Or
use following query to create userdetails
table in database and insert some data to bind values to dropdownlist.

createtable userdetails(

userid intprimarykeyidentity,

username varchar(50),

education varchar(50),

locationvarchar(50)

)

INSERTINTO userdetails(username,education,location)

values('Suresh Dasari','B.Tech','Chennai'),

('Rohini Alavala','Msc','Guntur'),

('Praveen Kumar','B.Tech','Bangalore'),

('Madhav Sai','MBA','Nagpur')

Once
we create userdetails table now
create asp.net mvc application for
that Open visual studio --> Go to
File --> Select New --> Project like as shown below

Once
we select Project new popup will open in that select Asp.Net Web Application
and give name to application and click OK like as shown below

Once
click OK new popup will open in that select Empty template and select folders and core reference as MVC and click OK like as shown below

Once
we finished creating application our project structure will be like as shown
below

Now
we will add new model to our
application to define properties in our application for that right click on Models folder --> select Add -->
select Class like as shown below

Once
we click on Class new popup will open in that give name of your model as “UserDetails” and click Add button like
as shown below.

Now
open newly created model (UserDetails) and write the code like as
shown below

using System.Collections.Generic;

namespace InsertGetUserDetails.Models

{

publicclassUserDetails

{

publicint UserId { get; set; }

publicstring UserName { get; set; }

publicstring Education { get; set; }

publicstring Location { get; set; }

publicList<UserDetails> usersinfo { get; set; }

}

}

Now we will add new
controller to get data from database for that right click on Controller folder
--> select Add --> Controller like as shown below

Once we click on Controller
new popup will open in that select MVC 5
Controller – Empty and click Add
like as shown below.

Once click on Add new
window will open in that enter name of controller and click Add like as shown
below

Now open newly created
controller and write the code like as shown below