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.
- 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;
- }
- }
- 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; }
- }
- 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();
- }
- @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 *@
- "viewport" content="width=device-width" />
-
Create Manager
- class="row">class="col-md-4">
- >
- //managername
"ModelOnly" class="text-danger">class="form-group">- ="ManagerName" class="control-label">
- for="ManagerName" class="form-control" />
- for="ManagerName" class="text-danger">
- //country
class="row">class="alert-danger" asp-validation-summary="ModelOnly">class="col-xs-12 col-sm-6 col-md-6 col-lg-4">- ="countryname" class="control-label">
- ="countryId"
- class="form-control"
- asp-items="@(new SelectList(@ViewBag.listofcountry,"countryId","countryname"))">
- //state
class="row">class="alert-danger" asp-validation-summary="ModelOnly">class="col-xs-12 col-sm-6 col-md-6 col-lg-4">- ="statename" class="control-label">
- ="stateId"
- class="form-control"
- asp-items="@(new SelectList(@ViewBag.listofstate,"stateId","statename"))">
- //city
class="row">class="alert-danger" asp-validation-summary="ModelOnly">class="col-xs-12 col-sm-6 col-md-6 col-lg-4">- ="cityname" class="control-label">
- ="cityId"
- class="form-control"
- asp-items="@(new SelectList(@ViewBag.listofcity,"cityId","cityname"))">
error : value cannot be null and another error:- asp-items="@(new SelectList(@ViewBag.listofcountry,"countryId","countryname"))">
- 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 *@
Image:Error


1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ramesh PalanivelPosted Oct 18, 2019, 3:50 AM