Bind Country List Using CultureInfo In ASP.NET MVC

In this article we will learn how to populate list of countries using C# globalization class which is capable to display the countries list.

Now let's start creating a simple MVC  application to demonstrate how to get list of countries without database in ASP.NET MVC using Globalization class

Step 1: Create an ASP.NET MVC Application.

Now let us start with a step by step approach from the creation of a simple MVC application as in the following,
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
  2. "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click OK. After clicking, the following window will appear,

Step 2: Add Controller Class

Now let us add the MVC 5 controller as in the following screenshot,

After clicking on Add button it will show the window. Specify the Controller name as Home with suffix Controller.
Now modify the default code in HomeController.cs class file and create the action methods, after modifying the code will look like as follows,
HomeController.cs
 
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;

namespace DisplayCountryList.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<string> CountryList = new List<string>() ;
            CultureInfo[] CInfoList = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo CInfo in CInfoList)
            {
                RegionInfo R = new RegionInfo(CInfo.LCID);
                if (!(CountryList.Contains(R.EnglishName)))
                {
                    CountryList.Add(R.EnglishName);
                }
            }

            CountryList.Sort();
            ViewBag.CountryList = CountryList;
            return View();
        }
    }
}

Step 3: Create View

Right click on view folder or near Index Action method and Add view with name Index and modify the existing view code , After modifying the code the Index.cshtml will be look like as follows

@{
    ViewBag.Title = "www.compilemode.com";
}
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        <div class="form-group">
            <label class="col-md-2 control-label">Select Country</label>
            <div class="col-md-10">
                @Html.DropDownList("CountryList",new SelectList(ViewBag.CountryList),new {@class = "form-control" })
            </div>
        </div>

        
    </div>
}

In the preceding code snippet we are binding DropDownList with ViewBag in which country list is assigned into the controller class.
Step 4: Run application
 Now run the application we will see dropdownlist having list of countries as shown in the following screen shot


Now Expand dropdownlist to see the other counties as shown in the following screen shot


I hope from all the preceding examples,  you learned how to display country list without Database in ASP.NET MVC.
Note:
  • Perform changes in code as per your requirement  its just example .
  • Follow proper standards which might be not meet with this given code.
  • It will not display all county list but its shows list major around 144 countries .
Summary
I hope this article is useful for all readers, If you have a suggestion then please contact me.
Don't Forget To 

1 comments:

How to select india by default?

Reply

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode