TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rahul Patil
1.6k
183
30.8k
Issue with DropDownlist in asp.net core mvc
Oct 17 2019 12:28 AM
DropDownList Data not inserted in database please someone help.
I m applying code first approach in asp.net core mvc
I am creating dropdown(country,state and city in table in mvc, but not inserted countryname,statename,cityname) in database see the image of inserted data in database.
Manager.cs
public
class
Manager {
[Key]
public
int
ManagerId {
get
;
set
;
}
public
string
ManagerName {
get
;
set
;
}
//Insertion in Manager table country state and city
public
int
countryId {
get
;
set
;
}
public
string
countryname {
get
;
set
;
}
public
int
stateId {
get
;
set
;
}
public
string
statename {
get
;
set
;
}
public
int
cityId {
get
;
set
;
}
public
string
cityname {
get
;
set
;
}
}
public
class
city {
public
int
cityId {
get
;
set
;
}
public
string
cityname {
get
;
set
;
}
[ForeignKey(
"state"
)]
public
int
stateId {
get
;
set
;
}
public
virtual
state states {
get
;
set
;
}
}
public
class
state {
public
int
stateId {
get
;
set
;
}
public
string
statename {
get
;
set
;
}
[ForeignKey(
"country"
)]
public
int
countryId {
get
;
set
;
}
public
virtual
country countrys {
get
;
set
;
}
}
public
class
country {
public
int
countryId {
get
;
set
;
}
public
string
countryname {
get
;
set
;
}
}
dbcontext.cs
public
class
dbcontext : IdentityDbContext
{
public
dbcontext(DbContextOptions options) :
base
(options)
{
Database.EnsureCreated();
}
protected
override
void
OnModelCreating(ModelBuilder builder)
{
base
.OnModelCreating(builder);
}
public
virtual
DbSet manages {
get
;
set
;}
public
virtual
DbSet countrys {
get
;
set
; }
public
virtual
DbSet states {
get
;
set
; }
public
virtual
DbSet citys {
get
;
set
; }
}
HomeController:
public
class
HomeController : Controller
{
private
readonly
dbcontext contxtclass;
public
HomeController(dbcontext contxtclass)
{
this
.contxtclass = contxtclass;
}
//not inserted countryname,stataname and cityname in database see the Referance image
public
IActionResult Create()
{
//bound the country table record
List countrylist =
new
List();
countrylist = (from countrys
in
contxtclass.countrys select countrys).ToList();
countrylist.Insert(0,
new
country { countryname =
"Select"
});
ViewBag.listofcountry = countrylist;
//bound the state table record
List statelist =
new
List();
statelist = (from states
in
contxtclass.states select states).ToList();
statelist.Insert(0,
new
state { statename =
"Select"
});
ViewBag.listofstate = statelist;
//bound the city table record
List citylist =
new
List();
citylist = (from citys
in
contxtclass.citys select citys).ToList();
citylist.Insert(0,
new
city { cityname =
"Select"
});
ViewBag.listofcity = citylist;
return
View();
}
[HttpPost]
public
IActionResult Create(Manager manager)
{
contxtclass.Add(manager);
contxtclass.SaveChanges();
return
View();
}
Create.cshtml
@model WebApplication2.Models.Manager
@{
Layout =
null
;
} @*here stop my debugger and give an error value can not be
null
*@
@* debugger can not check the below code *@
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Create</title>
</head>
<body>
<h4>Manager</h4>
<hr />
<div
class
=
"row"
>
<div
class
=
"col-md-4"
>
<form asp-action=
"Create"
>
//managername
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
></div>
<div
class
=
"form-group"
>
<label asp-
for
=
"ManagerName"
class
=
"control-label"
></label>
<input asp-
for
=
"ManagerName"
class
=
"form-control"
/>
<span asp-validation-
for
=
"ManagerName"
class
=
"text-danger"
></span>
</div>
//country
<div
class
=
"row"
>
<div
class
=
"alert-danger"
asp-validation-summary=
"ModelOnly"
></div>
<div
class
=
"col-xs-12 col-sm-6 col-md-6 col-lg-4"
>
<label asp-
for
=
"countryname"
class
=
"control-label"
></label>
<select asp-
for
=
"countryId"
class
=
"form-control"
asp-items=
"@(new SelectList(@ViewBag.listofcountry,"
countryId
","
countryname
"))"
></select>
</div>
</div>
//state
<div
class
=
"row"
>
<div
class
=
"alert-danger"
asp-validation-summary=
"ModelOnly"
></div>
<div
class
=
"col-xs-12 col-sm-6 col-md-6 col-lg-4"
>
<label asp-
for
=
"statename"
class
=
"control-label"
></label>
<select asp-
for
=
"stateId"
class
=
"form-control"
asp-items=
"@(new SelectList(@ViewBag.listofstate,"
stateId
","
statename
"))"
></select>
</div>
</div>
//city
<div
class
=
"row"
>
<div
class
=
"alert-danger"
asp-validation-summary=
"ModelOnly"
></div>
<div
class
=
"col-xs-12 col-sm-6 col-md-6 col-lg-4"
>
<label asp-
for
=
"cityname"
class
=
"control-label"
></label>
<select asp-
for
=
"cityId"
class
=
"form-control"
asp-items=
"@(new SelectList(@ViewBag.listofcity,"
cityId
","
cityname
"))"
></select>
</div>
</div>
error : value cannot be null and another error:
asp-items=
"@(new SelectList(@ViewBag.listofcountry,"
countryId
","
countryname
"))"
></select>
my debugger can not check code:
@{
Layout =
null
;
} @*here stop my debugger and give an error value can not be
null
*@
@* debugger can not check the below code html *@
<!DOCTYPE html>
Image:
Error
Reply
Answers (
1
)
Access values in list
Model binding with index view in mvc