Rahul Patil

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
  1. public class Manager {  
  2.  [Key]  
  3.  public int ManagerId {  
  4.   get;  
  5.   set;  
  6.  }  
  7.  public string ManagerName {  
  8.   get;  
  9.   set;  
  10.  }  
  11.   
  12.  //Insertion in Manager table country state and city    
  13.  public int countryId {  
  14.   get;  
  15.   set;  
  16.  }  
  17.  public string countryname {  
  18.   get;  
  19.   set;  
  20.  }  
  21.   
  22.  public int stateId {  
  23.   get;  
  24.   set;  
  25.  }  
  26.  public string statename {  
  27.   get;  
  28.   set;  
  29.  }  
  30.   
  31.  public int cityId {  
  32.   get;  
  33.   set;  
  34.  }  
  35.  public string cityname {  
  36.   get;  
  37.   set;  
  38.  }  
  39.   
  40. }  
  41. public class city {  
  42.  public int cityId {  
  43.   get;  
  44.   set;  
  45.  }  
  46.  public string cityname {  
  47.   get;  
  48.   set;  
  49.  }  
  50.   
  51.  [ForeignKey("state")]  
  52.  public int stateId {  
  53.   get;  
  54.   set;  
  55.  }  
  56.  public virtual state states {  
  57.   get;  
  58.   set;  
  59.  }  
  60. }  
  61. public class state {  
  62.  public int stateId {  
  63.   get;  
  64.   set;  
  65.  }  
  66.  public string statename {  
  67.   get;  
  68.   set;  
  69.  }  
  70.   
  71.  [ForeignKey("country")]  
  72.  public int countryId {  
  73.   get;  
  74.   set;  
  75.  }  
  76.  public virtual country countrys {  
  77.   get;  
  78.   set;  
  79.  }  
  80. }  
  81.   
  82. public class country {  
  83.  public int countryId {  
  84.   get;  
  85.   set;  
  86.  }  
  87.  public string countryname {  
  88.   get;  
  89.   set;  
  90.  }  
  91. }  
dbcontext.cs
  1. public class dbcontext : IdentityDbContext    
  2. {    
  3.     public dbcontext(DbContextOptions options) : base(options)    
  4.     {    
  5.         Database.EnsureCreated();    
  6.     }    
  7.   
  8.     protected override void OnModelCreating(ModelBuilder builder)     
  9.     {    
  10.         base.OnModelCreating(builder);    
  11.     }    
  12.   
  13.     public virtual DbSet manages {get;set;}    
  14.     public virtual DbSet countrys { getset; }    
  15.     public virtual DbSet states { getset; }    
  16.     public virtual DbSet citys { getset; }    
  17. }   
HomeController:
  1. public class HomeController : Controller    
  2. {    
  3.     private readonly dbcontext contxtclass;    
  4.   
  5.     public HomeController(dbcontext contxtclass)    
  6.     {    
  7.         this.contxtclass = contxtclass;    
  8.     }    
  9.   
  10.     //not inserted countryname,stataname and cityname in database see the Referance image    
  11.     public IActionResult Create()      
  12.     {    
  13.         //bound the country table record    
  14.         List countrylist = new List();    
  15.         countrylist = (from countrys in contxtclass.countrys select countrys).ToList();    
  16.         countrylist.Insert(0,new country {  countryname = "Select" });    
  17.         ViewBag.listofcountry = countrylist;    
  18.   
  19.         //bound the state table record    
  20.         List statelist = new List();    
  21.         statelist = (from states in contxtclass.states select states).ToList();    
  22.         statelist.Insert(0, new state { statename = "Select" });    
  23.         ViewBag.listofstate = statelist;    
  24.   
  25.         //bound the city table record    
  26.         List citylist = new List();    
  27.         citylist = (from citys in contxtclass.citys select citys).ToList();    
  28.         citylist.Insert(0, new city { cityname = "Select" });    
  29.         ViewBag.listofcity = citylist;    
  30.   
  31.         return View();    
  32.     }    
  33.   
  34.     [HttpPost]    
  35.     public IActionResult Create(Manager manager)    
  36.     {    
  37.         contxtclass.Add(manager);    
  38.         contxtclass.SaveChanges();    
  39.         return View();    
  40.     }   
Create.cshtml
  1. @model WebApplication2.Models.Manager    
  2.     
  3. @{    
  4.     Layout = null;    
  5. }  @*here stop my debugger and give an error value can not be null*@    
  6. @* debugger can not check the  below code *@    
  7. <!DOCTYPE html>    
  8.     
  9. <html>    
  10. <head>    
  11.     <meta name="viewport" content="width=device-width" />    
  12.     <title>Create</title>    
  13. </head>    
  14. <body>    
  15.     
  16. <h4>Manager</h4>    
  17. <hr />    
  18. <div class="row">    
  19.     <div class="col-md-4">    
  20.         <form asp-action="Create">    
  21.             //managername    
  22.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>    
  23.             <div class="form-group">    
  24.                 <label asp-for="ManagerName" class="control-label"></label>    
  25.                 <input asp-for="ManagerName" class="form-control" />    
  26.                 <span asp-validation-for="ManagerName" class="text-danger"></span>    
  27.             </div>    
  28.             //country    
  29.             <div class="row">    
  30.                 <div class="alert-danger" asp-validation-summary="ModelOnly"></div>    
  31.                 <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">    
  32.                     <label asp-for="countryname" class="control-label"></label>    
  33.                     <select asp-for="countryId"    
  34.                             class="form-control"    
  35.                             asp-items="@(new SelectList(@ViewBag.listofcountry,"countryId","countryname"))"></select>    
  36.                 </div>    
  37.             </div>    
  38.             //state    
  39.             <div class="row">    
  40.                 <div class="alert-danger" asp-validation-summary="ModelOnly"></div>    
  41.                 <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">    
  42.                     <label asp-for="statename" class="control-label"></label>    
  43.                     <select asp-for="stateId"    
  44.                             class="form-control"    
  45.                             asp-items="@(new SelectList(@ViewBag.listofstate,"stateId","statename"))"></select>    
  46.                 </div>    
  47.             </div>    
  48.             //city    
  49.             <div class="row">    
  50.                 <div class="alert-danger" asp-validation-summary="ModelOnly"></div>    
  51.                 <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">    
  52.                     <label asp-for="cityname" class="control-label"></label>    
  53.                     <select asp-for="cityId"    
  54.                             class="form-control"    
  55.                             asp-items="@(new SelectList(@ViewBag.listofcity,"cityId","cityname"))"></select>    
  56.                 </div>    
  57.             </div>  
error : value cannot be null and another error:
  1. asp-items="@(new SelectList(@ViewBag.listofcountry,"countryId","countryname"))"></select>  
  2. my debugger can not check code:  
  3. @{  
  4. Layout = null;  
  5. } @*here stop my debugger and give an error value can not be null*@  
  6. @* debugger can not check the below code html *@  
  7. <!DOCTYPE html>  
Image:
Error


Answers (1)