Skip to content
Loading
Dropdownlist showing list of all countries in asp.net mvc
0
  • Avnish Kumar
    Hi visit my git repo https://github.com/avikeid2007/GeoEssential
    It has example for ASP.net Core and Framework both with data for world Countries, states and Cities.
    0
  • hardcore Vishvjit i have not created any table field name there.
    0
  • Asma Khalid
    Look into this: https://bit.ly/2W8FHVL
     
    Mark the answer as yes if helpful.
     
    Thanks & Regards,
    Asma Khalid
     
    For more visit: www.asmak9.com
    0
  • Vishvjit Shinde
     Hi @Gcobani Mkontwana
      
    from ctry in objContext.Details select ctry
     
    objContext.Details is Context class.this is just an example.
     
    Tell me this. List of the country are coming from Database or you want to Hardcode?
     
    Read this Once you get An Idea:
    https://www.compilemode.com/2017/01/display-country-list-with-out-database-in-asp-net-mvc.html 
    0
  • Hi Vishvijit
     
    Here is my logic all together, im getting an error on objContext.Details not accessible. What type of libraries is to be used in it?
    1.   [HttpPost]  
    2.         [ValidateAntiForgeryToken]  
    3.         public async Task TraingRegForm(EditTrainingRegFormViewModel model)  
    4.         {  
    5.             RegionInfo country = new RegionInfo(new CultureInfo("en-ZAR"false).LCID);  
    6.             List<string> countries = new List<string>(2);  
    7.             var dtList = from ctry in objContext.Details select ctry;  // ObjContext does not exist in current context???
    8.   
    9.             foreach( var dt in dtList)  
    10.             {  
    11.                 countries.Add(dt.Name);  
    12.             }  
    13.             ViewBag.Name = countries;  
    14.             return View(dtList);  
    15.         }  
    16.   
    17. // Model class  
    18.  public class EditTrainingRegFormViewModel  
    19.     {  
    20.         public string Title { getset; }  
    21.         public string FirstName { getset; }  
    22.   
    23.         public string LastName { getset; }  
    24.   
    25.         public string Position { getset; }  
    26.   
    27.         public string Company { getset; }  
    28.   
    29.         public string Address { getset; }  
    30.   
    31.         public List <string> CountryList { getset; }  
    32.   
    33.         public string Cell_Number { getset; }  
    34.   
    35.         public List<string> Dietary_requirement { getset; }  
    36.   
    37.         public string Email { getset; }  
    38.          
    39.   
    40.     }  
    41.   
    42. // View  
    43.   
    44. @model eNtsaRegistrationTraining.Models.IndexViewModel  
    45.   
    46.   
    47. @{  
    48.     ViewBag.Title = "My Account";  
    49.     Layout = "~/Views/Shared/_Layout.cshtml";  
    50. }  
    51.   
    52. @{   
    53.     var CountryItems = new List();  
    54.     CountryItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });  
    55.     foreach(var name in ViewBag.Names)  
    56.     {  
    57.         CountryItems.Add(new SelectListItem { Text = name, Value = name });  
    58.     }  
    59. }  
    60.   
    61. class="content">  
    62.     class="container-fluid">  
    63.         class="row">  
    64.             class="col-md-6">  
    65.                 class="card card-primary">  
    66.                     "~/Content/dist/img/eNtsa.png" alt="" />  
    67.                 
      
  •                 @using(Html.BeginForm("editRegForm""Manage", FormMethod.Post, new {@role = "form"}))  
  •                 {  
  •                     @Html.AntiForgeryToken()  
  •                     class="card-body">  
  •                         class="form-group">  
  •                             for="title">Title  
  •                            @Html.TextBoxFor(m=>m.editRegForm.Title, new {@class = "form-control", type = "text", id="title", autofocus = "autofocus", placeholder = "Title"})  
  •                           
  •                         class="form-group">  
  •                             for="firstName">First Name  
  •                             @Html.TextBoxFor(m=>m.editRegForm.FirstName, new {@class = "form-control", type="text", id= "firstName", autofocus = "autofocus", placeholder = "First Name"})  
  •                           
  •                         class="form-group">  
  •                             for="lastName">Last Name  
  •                             @Html.TextBoxFor(m=>m.editRegForm.LastName, new {@class = "form-control", type="text", id="lastName", autofocus = "autofocus", placeholder = "Last Name"})  
  •                           
  •                         class="form-group">  
  •                             for="position">Position  
  •                             @Html.TextBoxFor(m=>m.editRegForm.Position, new {@class = "form-control", type="text", id="position", autofocus = "autofocus", placeholder = "Position"})  
  •                           
  •                         class="form-group">  
  •                             for="company">Company  
  •                             @Html.TextBoxFor(m=>m.editRegForm.Company, new {@class = "form-control", type = "text", id = "company", autofocus = "autofocus", placeholder = "Company"})                          
  •                           
  •                         class="form-group">  
  •                             for="address">Address  
  •                             @Html.TextBoxFor(m=>m.editRegForm, new {@class = "form-control", type="text", id="address", autofocus= "autofocus", placeholder = "Address"})  
  •                           
  •   
  •                       
  •                 }  
  •               
  •           
  •       
  •   
  •   
  •  
    0
  • Vishvjit Shinde
    @Gcobani Mkontwana
     
    Please use Concept of ViewBag Instead of that approach:
    Controller:
     
    1. var dtList = from ctry in objContext.Details select ctry; // Source of Country  
    2. foreach (var dt in dtList) {       
    3. countries.Add(dt.Name);   
    4. }   
    5. ViewBag.Names = countries ;   
    6. return View();  
     
    View: 
    @Html.DropDownList("CountryId"new SelectList(ViewBag.Names, "CountryID""CountryName"))       
     
    OR 
     
    Follow this article:
    https://www.c-sharpcorner.com/uploadfile/dpatra/getting-country-names-from-system/  
    0
  • Hi Vishvjit
     
    Ok thanks for the idea, please work with me here for my thought of doing it via the controller to the view within a form. please re-improve if my logic is not correct. 
    1. using using System.Globalization;  
    2.   
    3. using System.Collection;  
    4.   
    5. public actionResult ListCountries(string returnUrl) {  
    6.   
    7. List<string> countries = new List<string>(2);  
    8. var dtList = from ctry in objContext.Details.select ctry;  
    9.   
    10. foreach(var dt in dtList) {  
    11.   
    12. countries.Add(dt.Name);  
    13. }  
    14. return View();  
    15.   
    16.   
    17. // View  
    18.   
    19. ---Assume no reason to inherent model provided i have one created already here  
    20. @model....  
    21.   
    22.   
    23. @{  
    24.   
    25. var CountryItems = new List();  
    26. ..... continues as shown from article.  
    27. }  
    28. @Html.DropDownList("dropDownList", CountrItems)  
    The Question where do i put this logic if i have a form like this,that i want to create?
     
     
     
    0
  • Hi,
     
          Better You need to load all  countryId, countriesName and zipcode to the data base. Then retrive and show to the dropdown or chips like what ever you want. The country list db script available some open sources. 
     
    other solution: Load data's in Json locally.
     
    If you want full source code. show some sample of your code. 
    0
  • Vishvjit Shinde
    If you want to get country name from system check this: 
    https://www.c-sharpcorner.com/uploadfile/dpatra/getting-country-names-from-system/ 
     
     
     OR
    Take Country list from the database or as List of String   
    In Controller:
    List countries =new List(2); 
    var dtList = from ctry in objContext.Details select ctry; // Source of Country
    foreach (var dt in dtList) {     
    countries.Add(dt.Name); 
    } 
    ViewBag.Names = countries ; 
    return View(dtList);
     In View :
    @{ 
    var CountryItems = new List<SelectListItem>();
    CountryItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
     foreach (var name in ViewBag.Names)
    {
     CountryItems.Add(new SelectListItem { Text = name, Value = name }); 
    } 
    }
    @Html.DropDownList("dropDownList", CountryItems)
    0